-
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.
- Loading branch information
Showing
22 changed files
with
778 additions
and
827 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,134 @@ | ||
package checker | ||
|
||
// type Checker struct { | ||
// errors []error | ||
// } | ||
import ( | ||
"os" | ||
|
||
// func (check *Checker) Errors() []error { return check.errors } | ||
"github.com/davecgh/go-spew/spew" | ||
"github.com/saffage/jet/ast" | ||
"github.com/saffage/jet/internal/assert" | ||
"github.com/saffage/jet/types" | ||
) | ||
|
||
type Checker struct { | ||
types map[ast.Node]TypedValue | ||
defs map[*ast.Ident]Symbol | ||
uses map[*ast.Ident]Symbol | ||
|
||
module *Module | ||
scope *Scope | ||
builtIns []*BuiltIn | ||
errors []error | ||
isErrorHandled bool | ||
} | ||
|
||
func Check(node *ast.ModuleDecl) []error { | ||
module := NewModule(node) | ||
check := &Checker{ | ||
types: make(map[ast.Node]TypedValue), | ||
defs: make(map[*ast.Ident]Symbol), | ||
uses: make(map[*ast.Ident]Symbol), | ||
module: module, | ||
scope: module.scope, | ||
errors: make([]error, 0), | ||
isErrorHandled: true, | ||
} | ||
|
||
check.defBuiltIns() | ||
|
||
{ | ||
nodes := []ast.Node(nil) | ||
|
||
switch body := node.Body.(type) { | ||
case *ast.List: | ||
nodes = body.Nodes | ||
|
||
case *ast.CurlyList: | ||
nodes = body.List.Nodes | ||
|
||
default: | ||
panic("ill-formed AST") | ||
} | ||
|
||
for _, node := range nodes { | ||
ast.WalkTopDown(check.visit, node) | ||
} | ||
|
||
module.completed = true | ||
} | ||
|
||
{ | ||
f, err := os.Create("checker_state.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
spew.Fdump(f, check) | ||
} | ||
|
||
return check.errors | ||
} | ||
|
||
// Type checks 'expr' and returns its type. | ||
// If error was occured, result is undefined | ||
func (check *Checker) typeOf(expr ast.Node) types.Type { | ||
if t, ok := check.types[expr]; ok { | ||
return t.Type | ||
} | ||
|
||
if v := check.valueOfInternal(expr); v != nil { | ||
check.setValue(expr, *v) | ||
return v.Type | ||
} | ||
|
||
t := check.typeOfInternal(expr) | ||
check.setType(expr, t) | ||
return t | ||
} | ||
|
||
func (check *Checker) valueOf(expr ast.Node) *TypedValue { | ||
if t, ok := check.types[expr]; ok { | ||
return &t | ||
} | ||
|
||
if value := check.valueOfInternal(expr); value != nil { | ||
check.setValue(expr, *value) | ||
return value | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (check *Checker) setType(expr ast.Node, t types.Type) { | ||
assert.Ok(expr != nil) | ||
assert.Ok(t != nil) | ||
|
||
if check.types != nil { | ||
check.types[expr] = TypedValue{t, nil} | ||
} | ||
} | ||
|
||
func (check *Checker) setValue(expr ast.Node, value TypedValue) { | ||
assert.Ok(expr != nil) | ||
assert.Ok(value.Type != nil) | ||
|
||
if check.types != nil { | ||
check.types[expr] = value | ||
} | ||
} | ||
|
||
func (check *Checker) newDef(ident *ast.Ident, sym Symbol) { | ||
assert.Ok(ident != nil) | ||
|
||
if check.defs != nil { | ||
check.defs[ident] = sym | ||
} | ||
} | ||
|
||
func (check *Checker) newUse(ident *ast.Ident, sym Symbol) { | ||
assert.Ok(ident != nil) | ||
assert.Ok(sym != nil) | ||
|
||
if check.uses != nil { | ||
check.uses[ident] = sym | ||
} | ||
} |
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
Oops, something went wrong.