Exemple #1
0
 // A method for testing the other methods in the CompiledFunction class.
 public static bool UnitTest(bool verbose)
 {
     try {
         CompiledFunction cf = new CompiledFunction("test");
         cf.AddArgument("a");
         cf.AddArgument("b");
         cf.AddVariable("x");
         cf.AddVariable("y");
         cf.AddInstruction(OpCode.Load,1);
         int k = cf.AddInstruction(OpCode.Jump, 0);
         cf.AddInstruction(OpCode.Call, "getval", 0);
         int t = cf.getCodeOffset();
         cf.PatchInstruction(k, t);
         cf.AddInstruction(OpCode.Add);
     } catch( Exception e ) {
         Console.WriteLine("CompiledFunction: unit test failed; exception thrown:\n\t{0}",
             e.Message);
         return false;
     }
     Console.WriteLine("CompiledFunction: unit test succeeded");
     return true;
 }
Exemple #2
0
    // parses one function definition
    void matchFunction()
    {
        match(Token.KwdFunc);
        currFunction = new CompiledFunction(sc.TokenText);

        // Get the function text
        string fnName = sc.TokenText;
        match(Token.Ident);  // the function name

        // check for uniqueness, then add to the function list if unique.
        checkUniqueFunction(fnName);
        FunctionList.Add(fnName);

        currSymbolTable = new SymbolTable();
        match(Token.LPar);
        if (nextToken != Token.RPar) {
            for( ; ; ) {
                string argname = sc.TokenText;
                match(Token.Ident);
                if (!currSymbolTable.AddEntry(argname, currFunction.AddArgument(argname)))
                    SemanticError("duplicate declaration of argument {0}", argname);
                if (nextToken != Token.Comma)
                    break;
                advance();
            }
        }
        match(Token.RPar);
        matchBlock();
        // the function should end with a return statement.  In case ...
        currFunction.AddInstruction(OpCode.Stop);
        program.Add(currFunction);
        currFunction = null;
        currSymbolTable = null;
    }
Exemple #3
0
    // parses one function definition
    void matchFunction()
    {
        match(Token.KwdFunc);
        currFunction = new CompiledFunction(sc.TokenText);

        // Add next token to list
        string fn = sc.TokenText;

        int args = 0;
        Function func = new Function(fn, 0);

        if (!functionNames.Contains(fn))
            functionNames.Add(fn);
        else
            throw new ParseError("Duplicate function name " + func.name);

        match(Token.Ident);  // the function name
        currSymbolTable = new SymbolTable();
        match(Token.LPar);
        if (nextToken != Token.RPar) {
            for( ; ; ) {
                string argname = sc.TokenText;
                args++;
                match(Token.Ident);
                if (!currSymbolTable.AddEntry(argname, currFunction.AddArgument(argname)))
                    SemanticError("duplicate declaration of argument {0}", argname);
                if (nextToken != Token.Comma)
                    break;
                advance();
            }
        }

        func.args = args;
        functions.Add(func);

        match(Token.RPar);
        matchBlock();
        // the function should end with a return statement.  In case ...
        currFunction.AddInstruction(OpCode.Stop);
        program.Add(currFunction);
        currFunction = null;
        currSymbolTable = null;
    }