ScopeStmt* read_scopestmt () { AstTag tag = read_int(); switch(tag) { case VAR_STMT: { char* name = read_string(); Exp* exp = read_exp(); return make_ScopeVar(name, exp); } case FN_STMT: { char* name = read_string(); int nargs = read_int(); char** args = read_strings(nargs); ScopeStmt* body = read_scopestmt(); return make_ScopeFn(name, nargs, args, body); } case SEQ_STMT: { ScopeStmt* a = read_scopestmt(); ScopeStmt* b = read_scopestmt(); return make_ScopeSeq(a, b); } case EXP_STMT: { Exp* e = read_exp(); return make_ScopeExp(e); } default: { printf("Scope statement with unrecognized tag: %d\n", tag); exit(-1); } } return 0; }
int run_exp(const char *exp) { int error = 0; data_t *exp_list = read_exp(exp, NULL, &error); if(error) return error; eval(exp_list, the_global_env); return 0; }
SlotStmt* read_slot () { AstTag tag = read_int(); switch(tag) { case VAR_STMT: { char* name = read_string(); Exp* exp = read_exp(); return make_SlotVar(name, exp); } case FN_STMT: { char* name = read_string(); int nargs = read_int(); char** args = read_strings(nargs); ScopeStmt* body = read_scopestmt(); return make_SlotMethod(name, nargs, args, body); } default: { printf("Unrecognized slot with tag: %d\n", tag); exit(-1); } } }
/********************************************************************** * * get_args */ static int get_args(REG1 int argc, char* argv[]) { REG2 int opt; opterr = FALSE; /* handle errors ourselves */ while ((opt = getopt(argc, argv, "bdchilnoprstvxz123456789A:B:e:f:D")) != EOF) { switch (opt) { case 'A': trailing_context = (unsigned)atoi(optarg); break; case 'B': leading_context = (unsigned)atoi(optarg); break; case 'b': block_mode = TRUE; break; case 'd': no_boymoore = TRUE; break; case 'c': count = TRUE; break; case 'h': long_usage(); /* and exit */ case 'i': ignorecase = TRUE; type_bits |= RE_IGNORECASE; break; case 'l': names = TRUE; break; case 'n': number = TRUE; linecount_ptr = &linecount; break; case 'p': path_mode = TRUE; break; case 's': silent = TRUE; break; case 't': touch = names = TRUE; break; case 'v': nonmatch = TRUE; break; case 'x': exact = TRUE; break; case 'z': verbose = TRUE; block_mode = TRUE; break; case 'e': expr = optarg; break; case 'f': expr = read_exp(optarg); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': leading_context = trailing_context = opt - '0'; break; #ifdef TEST case 'D': debug++; break; #endif default: error("Invalid command line option, " "dgrep -h for help", 2); } } if (count || names || silent || touch) leading_context = trailing_context = 0; if (names || touch || silent) { block_mode = FALSE; verbose = FALSE; } return optind; }
Exp* read_exp () { AstTag tag = read_int(); switch(tag) { case INT_EXP: { int value = read_int(); return make_IntExp(value); } case NULL_EXP: { return make_NullExp(); } case PRINTF_EXP: { char* format = read_string(); int nexps = read_int(); Exp** exps = read_exps(nexps); return make_PrintfExp(format, nexps, exps); } case ARRAY_EXP: { Exp* length = read_exp(); Exp* init = read_exp(); return make_ArrayExp(length, init); } case OBJECT_EXP: { Exp* parent = read_exp(); int nslots = read_int(); SlotStmt** slots = read_slots(nslots); return make_ObjectExp(parent, nslots, slots); } case SLOT_EXP: { char* name = read_string(); Exp* exp = read_exp(); return make_SlotExp(name, exp); } case SET_SLOT_EXP: { char* name = read_string(); Exp* exp = read_exp(); Exp* value = read_exp(); return make_SetSlotExp(name, exp, value); } case CALL_SLOT_EXP: { char* name = read_string(); Exp* exp = read_exp(); int nargs = read_int(); Exp** args = read_exps(nargs); return make_CallSlotExp(name, exp, nargs, args); } case CALL_EXP: { char* name = read_string(); int nargs = read_int(); Exp** args = read_exps(nargs); return make_CallExp(name, nargs, args); } case SET_EXP: { char* name = read_string(); Exp* exp = read_exp(); return make_SetExp(name, exp); } case IF_EXP: { Exp* pred = read_exp(); ScopeStmt* conseq = read_scopestmt(); ScopeStmt* alt = read_scopestmt(); return make_IfExp(pred, conseq, alt); } case WHILE_EXP: { Exp* pred = read_exp(); ScopeStmt* body = read_scopestmt(); return make_WhileExp(pred, body); } case REF_EXP: { char* name = read_string(); return make_RefExp(name); } default: { printf("Expression with unrecognized tag: %d\n", tag); exit(-1); } } }