sema

package
v0.0.0-...-535c093 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2020 License: MIT Imports: 6 Imported by: 2

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

func AlphaTransform(tree *ast.AST, env *types.Env) error

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

func SemanticsCheck(parsed *ast.AST) (*types.Env, *mir.Block, error)

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:

func ToMIR

func ToMIR(root ast.Expr, env *types.Env, inferred InferredTypes, insts refInsts) *mir.Block

ToMIR converts given AST into MIR with type environment

func Unify

func Unify(left, right Type) *locerr.Error

Types

type Inferer

type Inferer struct {
	Env *Env
	// contains filtered or unexported fields
}

Inferer is a visitor to infer types in the AST

func NewInferer

func NewInferer(env *Env) *Inferer

NewInferer creates a new Inferer instance

func (*Inferer) Infer

func (inf *Inferer) Infer(parsed *ast.AST) error

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

type InferredTypes

type InferredTypes map[ast.Expr]Type

InferredTypes is a dictonary from an AST nodes to inferred types.

func Analyze

func Analyze(parsed *ast.AST) (*types.Env, InferredTypes, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL