better streamline

This commit is contained in:
2026-02-16 00:34:49 -06:00
parent dc440587ff
commit ff61ab1f50
5 changed files with 166 additions and 63 deletions

View File

@@ -291,6 +291,11 @@ var mcode = function(ast) {
emit_3("add", _bp_dest, _bp_left, _bp_right)
return null
}
// If either operand is a known number, concat is impossible
if (is_known_number(_bp_ln) || is_known_number(_bp_rn)) {
emit_numeric_binop("add")
return null
}
// Unknown types: emit full dispatch
var t0 = alloc_slot()
var t1 = alloc_slot()
@@ -1217,7 +1222,7 @@ var mcode = function(ast) {
}
// Binary expression compilation
var gen_binary = function(node) {
var gen_binary = function(node, target) {
var kind = node.kind
var left = node.left
var right = node.right
@@ -1272,7 +1277,8 @@ var mcode = function(ast) {
// Standard binary ops
left_slot = gen_expr(left, -1)
right_slot = gen_expr(right, -1)
dest = alloc_slot()
// Use target slot for ops without multi-type dispatch (add has text+num paths)
dest = (target >= 0 && kind != "+") ? target : alloc_slot()
op = binop_map[kind]
if (op == null) {
op = "add"
@@ -1426,9 +1432,9 @@ var mcode = function(ast) {
return val_slot
}
val_slot = gen_expr(right, -1)
left_kind = left.kind
// For local name assignments, try to write directly to the var's slot
if (left_kind == "name") {
name = left.name
level = left.level
@@ -1438,17 +1444,30 @@ var mcode = function(ast) {
if (level == 0 || level == -1) {
slot = find_var(name)
if (slot >= 0) {
emit_2("move", slot, val_slot)
} else if (level == -1) {
val_slot = gen_expr(right, slot)
if (val_slot != slot) {
emit_2("move", slot, val_slot)
}
return val_slot
}
val_slot = gen_expr(right, -1)
if (level == -1) {
add_instr(["set_var", name, val_slot])
}
} else if (level > 0) {
_lv = level - 1
pstate = parent_states[length(parent_states) - 1 - _lv]
pslot = find_var_in_saved(pstate, name)
emit_3("put", val_slot, pslot, level)
} else {
val_slot = gen_expr(right, -1)
if (level > 0) {
_lv = level - 1
pstate = parent_states[length(parent_states) - 1 - _lv]
pslot = find_var_in_saved(pstate, name)
emit_3("put", val_slot, pslot, level)
}
}
} else if (left_kind == ".") {
return val_slot
}
val_slot = gen_expr(right, -1)
if (left_kind == ".") {
obj = left.left
prop = left.right
obj_slot = gen_expr(obj, -1)
@@ -2045,7 +2064,7 @@ var mcode = function(ast) {
}
// Binary operators (fallback)
return gen_binary(expr)
return gen_binary(expr, target)
}
// Statement compilation