def parse(s): if not isinstance(s, list): raise ParseException if s[0] != sexpr.sym('define'): raise ParseException nameAndType = NameAndTypeDecl.parse(s[1]) expr = Expr.parse(s[2]) return GlobalValue(nameAndType, expr)
def parse(s): if isinstance(s, sexpr.sym): return NameAndTypeDecl(s.txt, NameAndTypeDecl.DEFAULT) elif isinstance(s, list): if len(s) == 3: if s[0] == sexpr.sym(':'): if isinstance(s[1], sexpr.sym): return NameAndTypeDecl(s[1].txt, Expr.parse(s[2])) raise ParseException, s
def parse(s): if not isinstance(s, list): raise ParseException if s[0] != sexpr.sym('define'): raise ParseException if not isinstance(s[1], list): raise ParseException nameAndType = NameAndTypeDecl.parse(s[1][0]) args = [] for a in s[1][1:]: fa = FunctionDefinition.FuncArg(NameAndTypeDecl.parse(a)) args.append(fa) body = Expr.parse(s[2]) return FunctionDefinition(nameAndType, args, body)
def parse(s): if not isinstance(s, list): raise ParseException if s[0] != sexpr.sym('let'): raise ParseException if not isinstance(s[1], list): raise ParseException, "expected list of let bindings" bindings = [] for binding in s[1]: if not isinstance(binding, list) or 2!=len(binding): raise ParseException, "not a valid let binding: %r" % binding nameAndType = NameAndTypeDecl.parse(binding[0]) expr = Expr.parse(binding[1]) bindings.append(LetBinding(nameAndType, expr)) body = Expr.parse(s[2]) return Let(bindings, body)
def returnTypeName(self): if self.nameAndType.isDefault: return sexpr.sym('s32') else: return self.nameAndType.declaredType.name
def llvmType(self): cc = CompileContext() tn = sexpr.sym('s32') if self.nameAndType.isDefault else self.nameAndType.declaredType.name t = cc.resolveSymbol(tn) return t.llvmType
def test_symbol(self): self.assertEqual(sym('asdf'), sexpr.parse1("asdf")) self.assertNotEqual(sym('asdf'), sexpr.parse1("asdf1")) self.assertTrue(sym('asdf') == sexpr.parse1("asdf")) self.assertTrue(sym('asdf') == sym('asdf')) self.assertTrue(sym('asdf') != sexpr.parse1("asdf1")) self.assertTrue(sym('asdf') != sym('asdf1')) self.assertTrue(sym('let') == sym('let')) self.assertFalse(sym('let') != sym('let'))
def test_big_expr(self): self.assertEqual(['asdf', [sym('bar'), sym('baz')], [123], [[['what', 123]]], 'foo'], sexpr.parse1("('asdf' (bar baz) (123) ((('what' 123))) 'foo')"))
def test_nested_with_elements(self): self.assertEqual([sym('a'), ['b', [3], 'b'], sym('a')], sexpr.parse1("(a ('b' (3) \"b\") a)"))