function error(msg) { printf("%s\n", msg) exit(1) } function bytes_to_int(bstr, i, sum) { sum = 0 for (i=word_size-1; i>=0; i--) { sum *= 256 sum += code[bstr+i] } return sum } function make_node(oper, left, right, value) { node_type [next_free_node_index] = oper node_left [next_free_node_index] = left node_right[next_free_node_index] = right node_value[next_free_node_index] = value return next_free_node_index ++ } function make_leaf(oper, n) { return make_node(oper, 0, 0, n) } function emit_byte(x) { code[next_free_code_index++] = x } function emit_word(x, i) { for (i=0; i 1) { value = line_list[2] for (i=3;i<=n;i++) value = value " " line_list[i] if (value ~ /^[0-9]+$/) value = int(value) return make_leaf(node_type, value) } left = load_ast() right = load_ast() return make_node(node_type, left, right) } BEGIN { all_syms["Identifier" ] = "nd_Ident" all_syms["String" ] = "nd_String" all_syms["Integer" ] = "nd_Integer" all_syms["Sequence" ] = "nd_Sequence" all_syms["If" ] = "nd_If" all_syms["Prtc" ] = "nd_Prtc" all_syms["Prts" ] = "nd_Prts" all_syms["Prti" ] = "nd_Prti" all_syms["While" ] = "nd_While" all_syms["Assign" ] = "nd_Assign" all_syms["Negate" ] = "nd_Negate" all_syms["Not" ] = "nd_Not" all_syms["Multiply" ] = "nd_Mul" all_syms["Divide" ] = "nd_Div" all_syms["Mod" ] = "nd_Mod" all_syms["Add" ] = "nd_Add" all_syms["Subtract" ] = "nd_Sub" all_syms["Less" ] = "nd_Lss" all_syms["LessEqual" ] = "nd_Leq" all_syms["Greater" ] = "nd_Gtr" all_syms["GreaterEqual"] = "nd_Geq" all_syms["Equal" ] = "nd_Eql" all_syms["NotEqual" ] = "nd_Neq" all_syms["And" ] = "nd_And" all_syms["Or" ] = "nd_Or" FETCH=1; STORE=2; PUSH=3; ADD=4; SUB=5; MUL=6; DIV=7; MOD=8; LT=9; GT=10; LE=11; GE=12; EQ=13; NE=14; AND=15; OR=16; NEG=17; NOT=18; JMP=19; JZ=20; PRTC=21; PRTS=22; PRTI=23; HALT=24; operators["nd_Lss"] = LT operators["nd_Gtr"] = GT operators["nd_Leq"] = LE operators["nd_Geq"] = GE operators["nd_Eql"] = EQ operators["nd_Neq"] = NE operators["nd_And"] = AND operators["nd_Or" ] = OR operators["nd_Sub"] = SUB operators["nd_Add"] = ADD operators["nd_Div"] = DIV operators["nd_Mul"] = MUL operators["nd_Mod"] = MOD unary_operators["nd_Negate"] = NEG unary_operators["nd_Not" ] = NOT next_free_node_index = 1 next_free_code_index = 0 globals_n = 0 string_n = 0 word_size = 4 input_file = "-" if (ARGC > 1) input_file = ARGV[1] n = load_ast() code_gen(n) code_finish() list_code() }