void initDefaultSyntaxContext( SyntaxContext* ctx ) { ctx->localSymbols = new( SymbolTable ); *ctx->localSymbols = newSymbolTable(); ctx->globalSymbols = new( SymbolTable ); *ctx->globalSymbols = newSymbolTable(); ctx->functions = NULL; addBuiltinToContext( ctx, makeRCString( "input" ), BIFinput, 0 ); addBuiltinToContext( ctx, makeRCString( "numeric" ), BIFnumeric, 1 ); addBuiltinToContext( ctx, makeRCString( "print" ), BIFprint, -1 ); addBuiltinToContext( ctx, makeRCString( "typeOf" ), BIFtypeOf, 1 ); addBuiltinToContext( ctx, makeRCString( "len" ), BIFlen, 1 ); addBuiltinToContext( ctx, makeRCString( "find" ), find, 2 ); addBuiltinToContext( ctx, makeRCString( "sort" ), sort, 1 ); }
struct symbolRecord* newClassSymbol ( struct symbolTableRecord* syms, struct symbolTableRecord* gsyms, char* name ) { // Make sure name is unique or forward referenced struct symbolRecord* s = lookupLocal(syms, name); struct typeRecord* t = 0; if (s == 0) // new name { s = newSymbolRecord(name, classDefSymbol); s->u.c.location = syms->size++; addNewSymbol(syms, s); t = newTypeRecord(classType); s->u.c.typ = t; } else // already defined { if (s->styp != classDefSymbol) { yyserror ( "non class name %s used to define class", s->name ); } t = s->u.c.typ; if (t == 0) { yyserror ( "compiler error, missing type in class %s", s->name ); } if (t->ttyp != classType) { yyserror("class %s has non class type field", s->name); } if (t->u.c.symbols) { yyserror("class %s multiply defined", s->name); } } struct symbolTableRecord* ns = newSymbolTable(classTable, syms); t->u.c.symbols = ns; ns->definingType = t; return s; }
int main(int argc, char const *argv[]) { ZAPPER_LIB_PATH = getenv("ZLIB_PATH"); if (ZAPPER_LIB_PATH == NULL) { printf("$ZLIB_PATH is not set. Using cwd for libraries and imports\n"); ZAPPER_LIB_PATH = "lib/"; } NAMESPACE = ""; // these next lines are used to read a file in for evaluation char *buffer = 0; long length; FILE *f; if (argc > 1) { f = fopen(argv[1], "rb"); if (f) { fseek(f, 0, SEEK_END); length = ftell(f); fseek(f, 0, SEEK_SET); buffer = malloc(length); if (buffer) { fread(buffer, 1, length, f); } fclose(f); } } else { CRASH_ON_EXCEPTION = 0; repl_init(); return 0; } if (buffer) { init_system(); symboltable_t* context = newSymbolTable(); list_t* expressions = parse(buffer)->value->value.l; object_t* value = eval(expressions, context); free(expressions); free(value); if (clib_functions->head != clib_functions->tail) { collecsymboltable_t(clib_functions); } } else { exception("No input file was specified.", newObject()); } return 0; }
struct symbolTableRecord* initialCreation() { struct symbolTableRecord* gs = newSymbolTable(globals, 0); // Create the constants NIL, true and false, but can't fill in types struct symbolRecord* nilsym = newSymbolRecord(newString("NIL"), varSymbol); nilsym->u.v.location = gs->size++; addNewSymbol(gs, nilsym); struct symbolRecord* truesym = newSymbolRecord(newString("true"), varSymbol); truesym->u.v.location = gs->size++; addNewSymbol(gs, truesym); struct symbolRecord* falsesym = newSymbolRecord(newString("false"), varSymbol); falsesym->u.v.location = gs->size++; addNewSymbol(gs, falsesym); // Create the initial classes objectType = makeInitialClass(gs, "object", 0); ClassType = makeInitialClass(gs, "Class", objectType); booleanType = makeInitialClass(gs, "boolean", objectType); integerType = makeInitialClass(gs, "integer", 0); realType = makeInitialClass(gs, "real", 0); stringType = makeInitialClass(gs, "string", objectType); trueType = makeInitialClass(gs, "True", booleanType); falseType = makeInitialClass(gs, "False", booleanType); undefinedType = makeInitialClass(gs, "Leda_undefined", objectType); // Now fill in types for symbols truesym->u.v.typ = trueType; falsesym->u.v.typ = falseType; nilsym->u.v.typ = undefinedType; // Finally, make the data-type relation relationType = newTypeRecord(functionType); struct symbolRecord* a = newSymbolRecord(newString("future"), argumentSymbol); a->u.a.location = 4; a->u.a.typ = relationType; a->u.a.form = byValue; relationType->u.f.argumentTypes = newList((char*) a, 0); relationType->u.f.returnType = booleanType; struct symbolRecord* s = newSymbolRecord(newString("relation"), typeSymbol); s->u.t.typ = relationType; addNewSymbol(gs, s); return gs; }
void repl_init() { char input[INPUT_SIZE] = ""; printf("%s", ">>> "); init_system(); symboltable_t* context = newSymbolTable(); while (EXIT_STATUS == 0 && fgets(input, INPUT_SIZE, stdin)) { list_t* expressions = parse(input)->value->value.l; object_t* value = eval(expressions, context); object_t* o = z_list(); z_conj(o->value->value.l, value); z_println(o->value->value.l); free(expressions); if (EXIT_STATUS == 0) { printf("%s", ">>> "); } } }
struct symbolTableRecord* addFunctionSymbol ( struct symbolTableRecord* syms, char* name, struct list* ta ) { struct symbolTableRecord* ns = newSymbolTable(functionTable, syms); struct symbolRecord* fs = lookupLocal(syms, name); if (fs && syms->ttype != globals) { // Name already in symbol table if (fs->styp != functionSymbol) { yyerror("non function name redefined as function"); } if (fs->u.f.inherited == 0) { yyerror("function multiply defined"); } fs->u.f.inherited = 0; // Now it is overridden fs->u.f.code = newStatement(nullStatement); } else // Enter name into symbol table { fs = newSymbolRecord(name, functionSymbol); fs->u.f.code = newStatement(nullStatement); switch(syms->ttype) { case functionTable: case globals: fs->u.f.location = syms->size++; addNewSymbol(syms, fs); break; case classTable: // Add as a method, not a variable fs->u.f.location = syms->u.c.methodTableSize++; fs->u.f.inherited = 0; syms->u.c.methodTable = newList((char*) fs, syms->u.c.methodTable); break; } } // Make an empty function type for defining type fs->u.f.typ = ns->definingType = newTypeRecord(functionType); // If there are type arguments, put them into the symbol table if (ta) { fs->u.f.typ = newQualifiedType(ns, ta, fs->u.f.typ); } // If a method, add ``self'' to the symbol table if (syms->ttype == classTable) { struct symbolRecord* s = newSymbolRecord(newString("self"), argumentSymbol); s->u.a.location = 1; s->u.a.typ = newConstantType(syms->definingType); addNewSymbol(ns, s); } // Put symbol into the function symbol table ns->u.f.theFunctionSymbol = fs; // Return the new function symbol return ns; }
object_t* z_eval(list_t* args) { return eval(args, newSymbolTable()); }