Documentation ¶
Overview ¶
Package sema provides resolving symbols, type inference and type check for GoCaml. Semantic check finally converts given AST into MIR (Mid-level IR). This package only provides type operations. To know data structures of types, please see https://godoc.org/github.com/rhysd/gocaml/types
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlphaTransform ¶
AlphaTransform adds identical names to all identifiers in AST nodes. If there are some duplicate names, it causes an error. External symbols are named the same as display names.
func SemanticsCheck ¶
SemanticsCheck applies type inference, checks semantics of types and finally converts AST into MIR with inferred type information.
Example ¶
file := filepath.FromSlash("../testdata/from-mincaml/ack.ml") src, err := locerr.NewSourceFromFile(file) if err != nil { // File not found panic(err) } ast, err := syntax.Parse(src) if err != nil { // When parse failed panic(err) } // Resolve symbols by alpha transform. // Then apply type inference. After this, all symbols in AST should have exact types. It also checks // types are valid and all types are determined by inference. It returns a type environment object // and converted MIR as the result. env, ir, err := SemanticsCheck(ast) if err != nil { // Type error detected panic(err) } // You can dump the type table env.Dump() ir.Println(os.Stdout, env)
Output:
Types ¶
type Inferer ¶
type Inferer struct { Env *Env // contains filtered or unexported fields }
Inferer is a visitor to infer types in the AST
func (*Inferer) Infer ¶
Infer infers types in given AST and returns error when detecting type errors
Example ¶
// Type check example // Analyzing target src, err := locerr.NewSourceFromFile(filepath.FromSlash("../testdata/from-mincaml/ack.ml")) if err != nil { // File not found panic(err) } parsed, err := syntax.Parse(src) if err != nil { // When parse failed fmt.Fprintln(os.Stderr, err) return } // Type environment for analysis env := types.NewEnv() // First, resolve all symbols by alpha transform if err := AlphaTransform(parsed, env); err != nil { fmt.Fprintln(os.Stderr, err) return } // Second, run unification on all nodes and dereference type variables // Make a visitor to do type inferernce inferer := NewInferer(env) // Do type inference. It returns error if type mismatch was detected. if err := inferer.Infer(parsed); err != nil { fmt.Fprintln(os.Stderr, err) return } // No error found! fmt.Println("OK")
Output: OK