From 3d02b6a591e4df70d16198c8a13703952a0ce3ba Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 12 Feb 2017 11:44:10 -0700 Subject: [PATCH] fix add, clean up main --- main.go | 13 +++++++++---- vm/cmds.go | 16 ++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index c839f3b..8eee009 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/vm/cmds.go b/vm/cmds.go index 518c9c2..e2247c0 100644 --- a/vm/cmds.go +++ b/vm/cmds.go @@ -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) }