a.silentErrors++;
case *ast.FuncDecl:
- log.Crash("FuncDecl at statement level");
+ if !a.block.global {
+ log.Crash("FuncDecl at statement level");
+ }
case *ast.GenDecl:
- switch decl.Tok {
- case token.IMPORT:
+ if decl.Tok == token.IMPORT && !a.block.global {
log.Crash("import at statement level");
- case token.CONST:
- log.Crashf("%v not implemented", decl.Tok);
- case token.TYPE:
- a.compileTypeDecl(a.block, decl);
- case token.VAR:
- a.compileVarDecl(decl);
}
default:
log.Crashf("Unexpected Decl type %T", s.Decl);
}
+ a.compileDecl(s.Decl);
}
-// decl might or might not be at top level;
func (a *stmtCompiler) compileVarDecl(decl *ast.GenDecl) {
for _, spec := range decl.Specs {
spec := spec.(*ast.ValueSpec);
}
}
-// decl is top level
func (a *stmtCompiler) compileDecl(decl ast.Decl) {
switch d := decl.(type) {
case *ast.BadDecl:
// Declare and initialize v before compiling func
// so that body can refer to itself.
c := a.block.DefineConst(d.Name.Value, a.pos, decl.Type, decl.Type.Zero());
- // TODO(rsc): How to mark v as constant
- // so the type checker rejects assignments to it?
fn := a.compileFunc(a.block, decl, d.Body);
if fn == nil {
return;
CErr("x := make(map[int] int); (func(a,b int){})(x[0])", "not enough"),
CErr("x := make(map[int] int); x[1] = oneTwo()", "too many"),
RErr("x := make(map[int] int); i = x[1]", "key '1' not found"),
+
+ // Functions
+ Val2("func fib(n int) int { if n <= 2 { return n } return fib(n-1) + fib(n-2) }", "fib(4)", 5, "fib(10)", 89),
}
func TestStmt(t *testing.T) {
return &stmtCode{w, fc.get()}, nil;
}
-func (w *World) compileDecls(decls []ast.Decl) (Code, os.Error) {
+func (w *World) compileDecls(decls []ast.Decl) (Code, os.Error) {
stmts := make([]ast.Stmt, len(decls));
for i, d := range decls {
stmts[i] = &ast.DeclStmt{d};
// Otherwise try as DeclList.
decls, err1 := parser.ParseDeclList("input", text);
- if err == nil {
+ if err1 == nil {
return w.compileDecls(decls);
}