Created
December 29, 2016 02:12
-
-
Save sarath-soman/2904263aa979867075277a3eacccbfdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ast | |
import ( | |
"fmt" | |
) | |
type NumericConstant struct { | |
Value string | |
Type int | |
} | |
type UnaryExpression struct { | |
Operator rune | |
Right Visitable | |
} | |
type BinaryExpression struct { | |
Left Visitable | |
Operator rune | |
Right Visitable | |
} | |
var OperatorConstant map[string]rune | |
var TypeConstant map[string]int | |
func init() { | |
OperatorConstant = make(map[string]rune) | |
OperatorConstant["+"] = '+' | |
OperatorConstant["-"] = '-' | |
OperatorConstant["*"] = '*' | |
OperatorConstant["/"] = '/' | |
TypeConstant = make(map[string]int) | |
TypeConstant["float"] = 1 | |
TypeConstant["int"] = 2 | |
TypeConstant["double"] = 3 | |
TypeConstant["uint"] = 4 | |
} | |
type Visitor interface { | |
VisitNumericConstant(num *NumericConstant) | |
VisitUnaryExpression(exp *UnaryExpression) | |
VisitBinaryExpression(exp *BinaryExpression) | |
} | |
type Visitable interface { | |
Accept(v *Visitor) | |
} | |
func (num *NumericConstant) Accept(v *Visitor){ | |
*v.VisitNumericConstant(num) | |
} | |
func (exp *UnaryExpression) Accept(v *Visitor){ | |
v.VisitUnaryExpression(exp) | |
} | |
func (exp *BinaryExpression) Accept(v *Visitor){ | |
v. | |
} | |
type Interpreter struct {} | |
func (i *Interpreter) VisitNumericConstant(num *NumericConstant) { | |
fmt.Printf(" %v ", num.Value) | |
} | |
func (i *Interpreter) VisitUnaryExpression(exp *UnaryExpression) { | |
fmt.Printf(" %v( ", exp.Operator) | |
exp.Right.Accept(i) | |
fmt.Printf(")") | |
} | |
func (i *Interpreter) VisitBinaryExpression(exp *BinaryExpression) { | |
exp.Left.Accept(i) | |
fmt.Printf(" %v ", exp.Operator) | |
exp.Right.Accept(i) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment