Example #1
0
        private static Type getFun(SymbolTable stable, CommonTree funId, List<Type> argTypes)
        {
            String sym = funId.Text;
            int line = funId.Line;
            if (!stable.exists(sym))
            {
                error(line, "function '" + sym + "' does not exist");
                return null;
            }

            Type fType = stable.getType(sym);
            if (!fType.isFun())
            {
                error(line, "'" + sym + "' is not a function");
                return null;
            }

            List<Type> formals = fType.getArgs();
            if (formals.Count != argTypes.Count)
            {
                error(line, "argument number mis-match when calling '" + sym + "' (need " + formals.Count + ", have " + argTypes.Count + ")");
                return null;
            }

            for (int i = 0; i < formals.Count; i++)
            {
                Type t1, t2;
                t1 = formals[i];
                t2 = argTypes[i];

                if (!t1.canAssign(t2))
                {
                    error(line, "arg " + i + " to function '" + sym + "' is not right");
                    return null;
                }
            }

            return fType.getReturnType();
        }
Example #2
0
        public static TransformStep<Tuple<CommonTokenStream, CommonTree>> TypeCheck()
        {
            return new TransformStep<Tuple<CommonTokenStream, CommonTree>>(t =>
            {
                CommonTreeNodeStream nodes = new CommonTreeNodeStream(t.Item2);
                nodes.TokenStream = t.Item1;
                TypeChecker tparser = new TypeChecker(nodes);
                tparser.TraceDestination = Console.Out;

                StructTypes stypes = new StructTypes();
                SymbolTable stable = new SymbolTable();

                Program.Stypes.Value = stypes;
                Program.Stable.Value = stable;

                tparser.Program(stypes, stable);

                if (tparser.NumberOfSyntaxErrors != 0)
                    throw new EvilException(EvilSystem.Typecheck, "Type check tree walking errors.");

                return t;
            });
        }
Example #3
0
 public SymbolTable(SymbolTable parent)
 {
     this.parent = parent;
     this.parent.children.Add(this);
 }
Example #4
0
 public void Program(StructTypes stypes, SymbolTable stable)
 {
     this.program(stypes, stable);
 }
Example #5
0
 private static Type getVar(SymbolTable stable, CommonTree sym)
 {
     if (stable.exists(sym.Text))
     {
         Type t = stable.getType(sym.Text);
         if (t.isFun())
             error(sym.Line, "sym '" + sym.Text + "' is a fun and that does not fly");
         return t;
     }
     error(sym.Line, "could not find symbol '" + sym.Text + "'");
     return null;
 }