fix add, clean up main

This commit is contained in:
Brian Picciano 2017-02-12 11:44:10 -07:00
parent 73d81dcbcc
commit 3d02b6a591
2 changed files with 17 additions and 12 deletions

13
main.go
View File

@ -8,10 +8,15 @@ import (
)
func main() {
t := lang.Tuple{vm.Add, lang.Tuple{vm.Tuple, lang.Tuple{
lang.Tuple{vm.Int, lang.Const("1")},
lang.Tuple{vm.Int, lang.Const("2")},
}}}
mkcmd := func(a lang.Atom, args ...lang.Term) lang.Tuple {
return lang.Tuple{a, lang.Tuple{vm.Tuple, lang.Tuple(args)}}
}
mkint := func(i string) lang.Tuple {
return lang.Tuple{vm.Int, lang.Const(i)}
}
t := mkcmd(vm.Add, mkint("1"),
mkcmd(vm.Add, mkint("2"), mkint("3")))
mod, err := vm.Build(t)
if err != nil {

View File

@ -20,6 +20,10 @@ type valType struct {
llvm llvm.Type
}
func (vt valType) isInt() bool {
return lang.Equal(Int, vt.term)
}
// most types don't have an input, so we use this as a shortcut
type voidIn struct{}
@ -92,7 +96,7 @@ func (tc tupCmd) build(mod *Module) (llvm.Value, error) {
type addCmd struct {
voidIn
a, b intCmd
a, b cmd
}
func (ac addCmd) outType() valType {
@ -166,14 +170,10 @@ func matchCmd(t lang.Term) (cmd, error) {
els, err := vAsTup(2)
if err != nil {
return nil, err
} else if _, ok := els[0].(intCmd); !ok {
return nil, errors.New("add args must be numbers")
} else if _, ok := els[1].(intCmd); !ok {
return nil, errors.New("add args must be numbers")
} else if !lang.Equal(els[0].outType().term, els[1].outType().term) {
return nil, errors.New("add args must be the same type")
} else if !els[0].outType().isInt() || !els[1].outType().isInt() {
return nil, errors.New("add args must be numbers of the same type")
}
return addCmd{a: els[0].(intCmd), b: els[1].(intCmd)}, nil
return addCmd{a: els[0], b: els[1]}, nil
default:
return nil, fmt.Errorf("cmd %v unknown, or its args are malformed", t)
}