-- src/catscript/compiler.cat -- the catscript compiler, written in catscript. -- -- Walks the AST (from parser.cat) and builds a *chunk table* the C -- runtime turns into runnable bytecode through bc_run/bc_dump. A chunk -- is: { name, nparams, nlocals, consts={}, protos={}, code={ {op,a,b,c,line} } }. -- -- Opcodes mirror include/cat/bytecode.h: -- 1 CONST 2 NIL 3 TRUE 4 FALSE 5 GETLOCAL 6 SETLOCAL 7 GETGLOBAL -- 8 SETGLOBAL 9 ADD 10 SUB 11 MUL 12 DIV 13 REM 14 NEG 15 NOT -- 16 EQ 17 NE 18 LT 19 LE 20 GT 21 GE 22 GETATTR 23 SETATTR -- 24 NEWTABLE 25 CALL 26 CALLMETHOD 27 JUMP 28 JUMPIF 29 JUMPIFNOT -- 30 RETURN 31 RETURN0 32 POP 33 PRINT 34 CLOSURE 35 GETUPVAL -- 36 CONCAT 37 DUP 38 GETIDX 39 SETIDX -- -- All state lives in globals prefixed CMP_ (closures capture no locals). CMP_CHUNK = nil CMP_CONSTS = {} CMP_NC = 0 CMP_PROTOS = {} CMP_NP = 0 CMP_CODE = {} CMP_NI = 0 CMP_LOCALS = {} CMP_NL = 0 CMP_LOOPS = {} -- stack of tables {breaks={...}} function cmp_emit(op, a, b, line) CMP_NI = CMP_NI + 1 CMP_CODE[CMP_NI] = { op = op, a = a, b = b, c = 0, line = line } return CMP_NI end -- patch the `a` operand of an already-emitted instruction (jump target) function cmp_patch(idx, target) local instr = CMP_CODE[idx] instr.a = target end function cmp_addconst(v) CMP_NC = CMP_NC + 1 CMP_CONSTS[CMP_NC] = v return CMP_NC - 1 -- 0-based index into the pool end function cmp_addstr(s) return cmp_addconst(s) end function cmp_findlocal(name) local i = CMP_NL while i >= 1 do if CMP_LOCALS[i] == name then return i - 1 end i = i - 1 end return -1 end function cmp_addlocal(name) CMP_NL = CMP_NL + 1 CMP_LOCALS[CMP_NL] = name return CMP_NL - 1 end -- enter a fresh function chunk; returns the previous state to restore function cmp_save() return { chunk = CMP_CHUNK, consts = CMP_CONSTS, nc = CMP_NC, protos = CMP_PROTOS, np = CMP_NP, code = CMP_CODE, ni = CMP_NI, locals = CMP_LOCALS, nl = CMP_NL, loops = CMP_LOOPS } end function cmp_restore(s) CMP_CHUNK = s.chunk; CMP_CONSTS = s.consts; CMP_NC = s.nc CMP_PROTOS = s.protos; CMP_NP = s.np; CMP_CODE = s.code; CMP_NI = s.ni CMP_LOCALS = s.locals; CMP_NL = s.nl; CMP_LOOPS = s.loops end function cmp_newchunk(name) CMP_CHUNK = nil CMP_CONSTS = {} CMP_NC = 0 CMP_PROTOS = {} CMP_NP = 0 CMP_CODE = {} CMP_NI = 0 CMP_LOCALS = {} CMP_NL = 0 CMP_LOOPS = {} CMP_CHUNK = { name = name, nparams = 0, nlocals = 0, consts = CMP_CONSTS, protos = CMP_PROTOS, code = CMP_CODE } return CMP_CHUNK end -- ---- expressions ----------------------------------------------------- function cmp_expr(e) local t = e.t if t == "nil" then cmp_emit(2, 0, 0, 0) elseif t == "true" then cmp_emit(3, 0, 0, 0) elseif t == "false" then cmp_emit(4, 0, 0, 0) elseif t == "int" then cmp_emit(1, cmp_addconst(e.v), 0, 0) elseif t == "float" then cmp_emit(1, cmp_addconst(e.v), 0, 0) elseif t == "str" then cmp_emit(1, cmp_addstr(e.v), 0, 0) elseif t == "name" then local slot = cmp_findlocal(e.v) if slot >= 0 then cmp_emit(5, slot, 0, 0) else cmp_emit(7, cmp_addstr(e.v), 0, 0) end elseif t == "bin" then cmp_expr(e.l) cmp_expr(e.r) cmp_emit(cmp_binop(e.op), 0, 0, 0) elseif t == "un" then cmp_expr(e.e) if e.op == "-" then cmp_emit(14, 0, 0, 0) elseif e.op == "not" then cmp_emit(15, 0, 0, 0) elseif e.op == "#" then cmp_emit(22, cmp_addstr("length"), 0, 0) end elseif t == "call" then cmp_expr(e.f) local i = 1 while i <= len(e.args) do cmp_expr(e.args[i]) i = i + 1 end cmp_emit(25, len(e.args), 0, 0) elseif t == "method" then cmp_expr(e.obj) local i = 1 while i <= len(e.args) do cmp_expr(e.args[i]) i = i + 1 end cmp_emit(26, len(e.args), cmp_addstr(e.name), 0) elseif t == "index" then cmp_expr(e.obj) cmp_expr(e.idx) cmp_emit(38, 0, 0, 0) elseif t == "attr" then cmp_expr(e.obj) cmp_emit(22, cmp_addstr(e.name), 0, 0) elseif t == "table" then cmp_emit(24, 0, 0, 0) local pos = 1 local i = 1 while i <= len(e.fields) do local f = e.fields[i] if f.k ~= nil then cmp_emit(37, 0, 0, 0) -- DUP obj cmp_expr(f.v) cmp_emit(23, cmp_addstr(f.k), 0, 0) else cmp_emit(37, 0, 0, 0) cmp_emit(1, cmp_addconst(pos), 0, 0) cmp_expr(f.v) cmp_emit(39, 0, 0, 0) pos = pos + 1 end i = i + 1 end elseif t == "func" then local proto = cmp_func(e) cmp_emit(34, proto, 0, 0) end end function cmp_binop(op) if op == "+" then return 9 end if op == "-" then return 10 end if op == "*" then return 11 end if op == "/" then return 12 end if op == "%" then return 13 end if op == ".." then return 36 end if op == "==" then return 16 end if op == "~=" then return 17 end if op == "<" then return 18 end if op == "<=" then return 19 end if op == ">" then return 20 end if op == ">=" then return 21 end return 9 end function cmp_func(f) local saved = cmp_save() cmp_newchunk(f.name) CMP_CHUNK.nparams = len(f.params) local i = 1 while i <= len(f.params) do cmp_addlocal(f.params[i]) i = i + 1 end cmp_block(f.body) cmp_emit(31, 0, 0, 0) -- implicit RETURN0 local chunk = CMP_CHUNK cmp_restore(saved) CMP_NP = CMP_NP + 1 CMP_PROTOS[CMP_NP] = chunk return CMP_NP - 1 end -- ---- statements ------------------------------------------------------ function cmp_block(stats) if stats == nil then return end local i = 1 while i <= len(stats) do cmp_stat(stats[i]) i = i + 1 end end function cmp_stat(s) local k = s.t if k == "local" then if s.init ~= nil then cmp_expr(s.init) else cmp_emit(2, 0, 0, 0) end local slot = cmp_addlocal(s.name) cmp_emit(6, slot, 0, 0) elseif k == "assign" then cmp_expr(s.value) local slot = cmp_findlocal(s.name) if slot >= 0 then cmp_emit(6, slot, 0, 0) else cmp_emit(8, cmp_addstr(s.name), 0, 0) end elseif k == "setidx" then local slot = cmp_findlocal(s.name) if slot >= 0 then cmp_emit(5, slot, 0, 0) else cmp_emit(7, cmp_addstr(s.name), 0, 0) end cmp_expr(s.idx) cmp_expr(s.value) cmp_emit(39, 0, 0, 0) elseif k == "setattr" then local slot = cmp_findlocal(s.name) if slot >= 0 then cmp_emit(5, slot, 0, 0) else cmp_emit(7, cmp_addstr(s.name), 0, 0) end cmp_expr(s.value) cmp_emit(23, cmp_addstr(s.attr), 0, 0) elseif k == "funcdef" then local proto = cmp_func(s.func) cmp_emit(34, proto, 0, 0) cmp_emit(8, cmp_addstr(s.name), 0, 0) elseif k == "if" then cmp_if(s) elseif k == "while" then cmp_while(s) elseif k == "repeat" then cmp_repeat(s) elseif k == "for" then cmp_for(s) elseif k == "return" then if s.e ~= nil then cmp_expr(s.e) cmp_emit(30, 0, 0, 0) else cmp_emit(31, 0, 0, 0) end elseif k == "expr" then cmp_expr(s.e) cmp_emit(32, 0, 0, 0) end end function cmp_if(s) local ends = {} local nends = 0 local i = 1 local jf = -1 while i <= len(s.conds) do if i > 1 then nends = nends + 1 ends[nends] = cmp_emit(27, 0, 0, 0) cmp_patch(jf, CMP_NI) end cmp_expr(s.conds[i].cond) jf = cmp_emit(29, 0, 0, 0) cmp_block(s.conds[i].body) i = i + 1 end if s.els ~= nil then nends = nends + 1 ends[nends] = cmp_emit(27, 0, 0, 0) cmp_patch(jf, CMP_NI) cmp_block(s.els) else cmp_patch(jf, CMP_NI) end local k = 1 while k <= nends do cmp_patch(ends[k], CMP_NI) k = k + 1 end end function cmp_while(s) local jcond = CMP_NI + 1 cmp_expr(s.cond) local jend = cmp_emit(29, 0, 0, 0) CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} } cmp_block(s.body) local loop = CMP_LOOPS[len(CMP_LOOPS)] CMP_LOOPS[len(CMP_LOOPS)] = nil cmp_emit(27, jcond, 0, 0) cmp_patch(jend, CMP_NI) local i = 1 while i <= len(loop.breaks) do cmp_patch(loop.breaks[i], CMP_NI) i = i + 1 end end function cmp_repeat(s) local jtop = CMP_NI + 1 CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} } cmp_block(s.body) local loop = CMP_LOOPS[len(CMP_LOOPS)] CMP_LOOPS[len(CMP_LOOPS)] = nil cmp_expr(s.cond) cmp_emit(29, jtop, 0, 0) local i = 1 while i <= len(loop.breaks) do cmp_patch(loop.breaks[i], CMP_NI) i = i + 1 end end function cmp_for(s) cmp_expr(s.from) if s.step ~= nil then cmp_expr(s.step) else cmp_emit(1, cmp_addconst(1), 0, 0) end cmp_expr(s.to) -- stack now: from, step, to (top is to) local slot = cmp_addlocal(s.var) cmp_addlocal(s.var .. " .limit") cmp_addlocal(s.var .. " .step") cmp_emit(6, slot + 2, 0, 0) -- step cmp_emit(6, slot + 1, 0, 0) -- limit cmp_emit(6, slot, 0, 0) -- var local jcond = CMP_NI + 1 cmp_emit(5, slot, 0, 0) cmp_emit(5, slot + 1, 0, 0) cmp_emit(19, 0, 0, 0) local jend = cmp_emit(29, 0, 0, 0) CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} } cmp_block(s.body) local loop = CMP_LOOPS[len(CMP_LOOPS)] CMP_LOOPS[len(CMP_LOOPS)] = nil cmp_emit(5, slot, 0, 0) cmp_emit(5, slot + 2, 0, 0) cmp_emit(9, 0, 0, 0) cmp_emit(6, slot, 0, 0) cmp_emit(27, jcond, 0, 0) cmp_patch(jend, CMP_NI) local i = 1 while i <= len(loop.breaks) do cmp_patch(loop.breaks[i], CMP_NI) i = i + 1 end end function cmp_compile(ast) cmp_newchunk("main") cmp_block(ast.body) cmp_emit(31, 0, 0, 0) CMP_CHUNK.nlocals = CMP_NL return CMP_CHUNK end