Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            PointerVarType v2 = (obj as PointerVarType);

            if (v2 == null)
            {
                return(false);
            }
            return(pointedToType.Equals(v2.pointedToType));
        }
Ejemplo n.º 2
0
    static void funcdeclNodeCode(TreeNode n, bool justPutFunctionNamesInSymtable)
    {
        var     fname = n.Children[1].Lexeme;
        VarType retType;

        optionalreturnspecNodeCode(n.Children[5], out retType);
        List <VarType> argTypes;
        List <string>  argNames;

        optionaltypelistNodeCode(n.Children[3], out argNames, out argTypes);

        for (int i = 0; i < argTypes.Count; ++i)
        {
            if (argTypes[i] as ArrayVarType != null)
            {
                argTypes[i] = new PointerVarType(argTypes[i]);
            }
        }

        var funcType = new FuncVarType(argTypes, retType);

        if (justPutFunctionNamesInSymtable)
        {
            if (symtable.ContainsInCurrentScope(fname))
            {
                throw new Exception("Variable already exists.");
            }
            symtable[fname] = new VarInfo(funcType, label(), false);
        }
        else
        {
            symtable.AddScope();
            addParametersToSymbolTable(argNames, argTypes);
            emit("{0}:", symtable[fname].Label);
            prologueCode();
            braceblockNodeCode(n.Children[6], 0);
            epilogueCode();
            symtable.DeleteScope();
        }
    }
Ejemplo n.º 3
0
    static void factorNodeCode(TreeNode n, out VarType type)
    {
        string vname;
        var    child = n.Children[0];

        switch (child.Symbol)
        {
        case "NUM":
            double d  = Convert.ToDouble(child.Lexeme);
            string ds = d.ToString("f");
            if (ds.IndexOf(".") == -1)
            {
                ds += ".0";
            }
            emit("mov rax,__float64__({0})", ds);
            emit("push rax");
            type = VarType.NUMBER;
            break;

        case "LP":
            exprNodeCode(n.Children[1], out type);
            break;

        case "STRING-CONSTANT":
            string lbl;
            stringconstantNodeCode(child, out lbl);
            emit("mov rax,{0}", lbl);
            emit("push rax");
            type = VarType.STRING;
            break;

        case "ID":
            vname = n.Children[0].Lexeme;
            if (symtable[vname] == null)
            {
                throw new Exception("Undeclared Variable.");
            }

            VarInfo vi = symtable[vname];
            if (vi.VType == VarType.NUMBER || vi.VType == VarType.STRING)
            {
                emit("mov rax,[{0}]", symtable[vname].Label);
                emit("push rax");
                type = vi.VType;
            }
            else if (vi.VType as ArrayVarType != null)
            {
                emit("lea rax,[{0}]", symtable[vname].Label);
                emit("push rax");
                type = new PointerVarType(vi.VType);
            }
            else
            {
                throw new Exception("Internal Compiler Error.");
            }
            break;

        case "func-call":
            funccallNodeCode(child, out type);
            if (type == VarType.VOID)
            {
                throw new Exception("No void math expresions.");
            }
            emit("push rax");
            break;

        case "array-access":
            vname = child.Children[0].Lexeme;
            var vinfo = symtable[vname];
            if (vinfo == null)
            {
                throw new Exception("Undeclared varaible");
            }
            var atype = vinfo.VType as ArrayVarType;
            if (atype == null)
            {
                atype = (vinfo.VType as PointerVarType).pointedToType as ArrayVarType;
                if (atype == null)
                {
                    throw new Exception("Variable is not an Array.");
                }
            }

            putArrayAddressInRcx(vinfo, n.Children[0].Children[2]);
            emit("mov rax,[rcx]");
            emit("push rax");
            type = atype.baseType;
            break;

        default:
            throw new Exception("Internal Compiler Error.");
        }
    }