public void handleFunction(ASTFunction node) { // Get the function's symbol table, set it's previous to the // calling function's, and then set it to current. connector.startSnap(node.getLineNumber()); if (node.getName().equals("main")) { connector.addQuestion(startQuestion); connector.showScope("main"); } else { } connector.endSnap(); if (!node.getUsed()) { return; } SymbolTable currentSymbolTable = node.getSymbolTable(); for (String p : node.getParameters()) { ByNameVariable v = new ByNameVariable(); v.setParam(); currentSymbolTable.put(p, v); } Global.setCurrentSymbolTable(currentSymbolTable); node.jjtGetChild(0).jjtAccept(this, null); leaveScope(); }
/** * Define a real-valued function of any number of real arguments by parsing its definition. * * @param name the name of the function. This can be null and really only has to be non-null if * the function will be added to a Parser for use in other expressions. * @param definition The string that will be parsed to define the function. * @param argumentName The names of the arguments to the function. There can be any number of * argumentNames. They must be non-null and should be legal identifiers. * @return the function. If the number of arguments is one, two, or three, then the return value * is actually of type {@link Function1}, {@link Function2}, or {@link Function3}, * respectively. * @throws ParseError if a syntax error is found in the definition of the function. */ public Function parseFunction(String name, String definition, String... argumentName) { Context context; int argCount; if (argumentName != null && argumentName.length > 0) { argCount = argumentName.length; SymbolTable tbl = new SymbolTable(symbolTable); for (int i = 0; i < argumentName.length; i++) tbl.put(argumentName[i].toLowerCase(), new Argument(i)); context = new Context(definition, tbl, false); } else { argCount = 0; context = new Context(definition, symbolTable, false); } context.bldr.start(argCount, Type.REAL); Type type = doParse(context); if (type == Type.COMPLEX) error(context, "vmm.parser.ExpectedRealFoundComplex"); if (type == Type.BOOLEAN) error(context, "vmm.parser.ExpectedRealFoundBoolean"); ProgFunction func = context.bldr.finish(Type.REAL); if (argCount == 1) return new Function1(name, func); else if (argCount == 2) return new Function2(name, func); else if (argCount == 3) return new Function3(name, func); else return new Function(name, func); }
private void define(String id, Type type, CommonTree decl) { // Add id with its type to the type table, checking // that id is not already declared in the same scope. boolean ok = typeTable.put(id, type); if (!ok) reportError(id + " is redeclared", decl); }
private void predefine() { // Add predefined procedures to the type table. typeTable.put("read", new Type.Mapping(Type.VOID, Type.INT)); typeTable.put("write", new Type.Mapping(Type.INT, Type.VOID)); }