-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interpret variables assignment and evaluation.
- Loading branch information
1 parent
174c23d
commit 4175b71
Showing
4 changed files
with
92 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,46 @@ | ||
package glox | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type AstPrinter struct{} | ||
|
||
func (ap *AstPrinter) Print(expr Expr) (string, error) { | ||
val, err := expr.Accept(ap) | ||
if err != nil { | ||
return "", err | ||
} | ||
// func (ap *AstPrinter) Print(expr Expr) (string, error) { | ||
// val, err := expr.Accept(ap) | ||
// if err != nil { | ||
// return "", err | ||
// } | ||
|
||
return val.(string), nil | ||
} | ||
// return val.(string), nil | ||
// } | ||
|
||
func (ap *AstPrinter) VisitBinaryExpr(expr *Binary) (interface{}, error) { | ||
return ap.parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right), nil | ||
} | ||
// func (ap *AstPrinter) VisitBinaryExpr(expr *Binary) (interface{}, error) { | ||
// return ap.parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right), nil | ||
// } | ||
|
||
func (ap *AstPrinter) VisitGroupingExpr(expr *Grouping) (interface{}, error) { | ||
return ap.parenthesize("group", expr.Expression), nil | ||
} | ||
// func (ap *AstPrinter) VisitGroupingExpr(expr *Grouping) (interface{}, error) { | ||
// return ap.parenthesize("group", expr.Expression), nil | ||
// } | ||
|
||
func (ap *AstPrinter) VisitLiteralExpr(expr *Literal) (interface{}, error) { | ||
if expr.Value == nil { | ||
return "nil", nil | ||
} | ||
// func (ap *AstPrinter) VisitLiteralExpr(expr *Literal) (interface{}, error) { | ||
// if expr.Value == nil { | ||
// return "nil", nil | ||
// } | ||
|
||
return fmt.Sprintf("%v", expr.Value), nil | ||
} | ||
// return fmt.Sprintf("%v", expr.Value), nil | ||
// } | ||
|
||
func (ap *AstPrinter) VisitUnaryExpr(expr *Unary) (interface{}, error) { | ||
return ap.parenthesize(expr.Operator.Lexeme, expr.Right), nil | ||
} | ||
// func (ap *AstPrinter) VisitUnaryExpr(expr *Unary) (interface{}, error) { | ||
// return ap.parenthesize(expr.Operator.Lexeme, expr.Right), nil | ||
// } | ||
|
||
func (ap *AstPrinter) parenthesize(name string, exprs ...Expr) string { | ||
s := strings.Builder{} | ||
s.WriteString("(" + name) | ||
// func (ap *AstPrinter) parenthesize(name string, exprs ...Expr) string { | ||
// s := strings.Builder{} | ||
// s.WriteString("(" + name) | ||
|
||
for _, expr := range exprs { | ||
s.WriteString(" ") | ||
val,_ := expr.Accept(ap) | ||
s.WriteString(val.(string)) | ||
} | ||
// for _, expr := range exprs { | ||
// s.WriteString(" ") | ||
// val,_ := expr.Accept(ap) | ||
// s.WriteString(val.(string)) | ||
// } | ||
|
||
s.WriteString(")") | ||
return s.String() | ||
} | ||
// s.WriteString(")") | ||
// return s.String() | ||
// } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package glox | ||
|
||
type Environment struct { | ||
// values uses string for the keys and not Token because token represents | ||
// a unit of code at a specific place in the source text, but when it comes | ||
// to variables, all identifier tokens using the same name should refer to | ||
// the same variable (ignorig scope for now). | ||
values map[string]interface{} | ||
} | ||
|
||
func NewEnvironment() *Environment { | ||
return &Environment{values: make(map[string]interface{}, 0)} | ||
} | ||
|
||
func (e *Environment) Define(name string, value interface{}) { | ||
e.values[name] = value | ||
} | ||
|
||
func (e *Environment) Get(name Token) (interface{}, error) { | ||
val, ok := e.values[name.Lexeme] | ||
if !ok { | ||
return nil, NewRuntimeError(name, "Undefined variable '" + name.Lexeme + "'") | ||
} | ||
|
||
return val, nil | ||
} |
This file contains 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
This file contains 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