Language: named colors, namespaced Screen/Input/Random/Map API, named args
An opinionated, game-facing surface for the language: - Color.<Name>: 221 named colors resolved to 0xRRGGBB at compile time (selfhost/emit_color.ludic). - Namespaced builtins Screen.* / Input.key / Random.* / Map.*, so calls read as subject.action; present() -> Screen.show(), clear -> Screen.clear, fill_rect -> Screen.fill_rectangle, etc. - Named arguments, e.g. Screen.fill_rectangle(x:, y:, width:, height:, color:), reordered to the callee's parameters at emit time. - machine/become can run over a named program-scope var, not just a register. - Migrate examples off numeric registers to named vars; snake/menu/chronorift render byte-identical to the goldens and the bootstrap fixpoint is preserved. - Regenerate the checked-in seed (selfhost/ludicc.seed.ll). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
9e4837428c
commit
a3a1e4d160
14 changed files with 11784 additions and 7493 deletions
236
selfhost/emit_color.ludic
Normal file
236
selfhost/emit_color.ludic
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
# ============================================================================
|
||||
# emit_color.ludic — the named-color palette, resolved at compile time.
|
||||
#
|
||||
# `Color.Name` in a game lowers to a plain 0xRRGGBB int here: no runtime cost,
|
||||
# no allocation, identical codegen to writing the hex by hand. Unknown names are
|
||||
# a compile error (color_lookup returns -1, which emit_expr reports).
|
||||
#
|
||||
# GENERATED by scratchpad/palette.py from the single source-of-truth palette.
|
||||
# Edit the palette there and regenerate; do not hand-edit this file.
|
||||
# ============================================================================
|
||||
|
||||
fn color_lookup(name: ptr) -> int {
|
||||
if (name == "White") { return 0xFFFFFF }
|
||||
if (name == "Snow") { return 0xFFFAFA }
|
||||
if (name == "Ivory") { return 0xFFFFF0 }
|
||||
if (name == "EggShellWhite") { return 0xF0EAD6 }
|
||||
if (name == "FloralWhite") { return 0xFFFAF0 }
|
||||
if (name == "SeaShell") { return 0xFFF5EE }
|
||||
if (name == "Linen") { return 0xFAF0E6 }
|
||||
if (name == "AntiqueWhite") { return 0xFAEBD7 }
|
||||
if (name == "OldLace") { return 0xFDF5E6 }
|
||||
if (name == "Beige") { return 0xF5F5DC }
|
||||
if (name == "Cream") { return 0xFFFDD0 }
|
||||
if (name == "Honeydew") { return 0xF0FFF0 }
|
||||
if (name == "MintCream") { return 0xF5FFFA }
|
||||
if (name == "Azure") { return 0xF0FFFF }
|
||||
if (name == "AliceBlue") { return 0xF0F8FF }
|
||||
if (name == "GhostWhite") { return 0xF8F8FF }
|
||||
if (name == "WhiteSmoke") { return 0xF5F5F5 }
|
||||
if (name == "Lavender") { return 0xE6E6FA }
|
||||
if (name == "Bone") { return 0xE3DAC9 }
|
||||
if (name == "Parchment") { return 0xF1E9D2 }
|
||||
if (name == "Gainsboro") { return 0xDCDCDC }
|
||||
if (name == "LightGray") { return 0xD3D3D3 }
|
||||
if (name == "Silver") { return 0xC0C0C0 }
|
||||
if (name == "Ash") { return 0xB2BEB5 }
|
||||
if (name == "DarkGray") { return 0xA9A9A9 }
|
||||
if (name == "Gray") { return 0x808080 }
|
||||
if (name == "DimGray") { return 0x696969 }
|
||||
if (name == "Nickel") { return 0x727472 }
|
||||
if (name == "Slate") { return 0x708090 }
|
||||
if (name == "SlateGray") { return 0x708090 }
|
||||
if (name == "LightSlateGray") { return 0x778899 }
|
||||
if (name == "Gunmetal") { return 0x2A3439 }
|
||||
if (name == "Charcoal") { return 0x36454F }
|
||||
if (name == "Graphite") { return 0x1C1C1C }
|
||||
if (name == "Onyx") { return 0x353839 }
|
||||
if (name == "Jet") { return 0x343434 }
|
||||
if (name == "Black") { return 0x000000 }
|
||||
if (name == "EerieBlack") { return 0x1B1B1B }
|
||||
if (name == "RaisinBlack") { return 0x242124 }
|
||||
if (name == "Ebony") { return 0x555D50 }
|
||||
if (name == "Red") { return 0xFF0000 }
|
||||
if (name == "Crimson") { return 0xDC143C }
|
||||
if (name == "Scarlet") { return 0xFF2400 }
|
||||
if (name == "Vermilion") { return 0xE34234 }
|
||||
if (name == "FireBrick") { return 0xB22222 }
|
||||
if (name == "Cinnabar") { return 0xE44D2E }
|
||||
if (name == "DarkRed") { return 0x8B0000 }
|
||||
if (name == "Maroon") { return 0x800000 }
|
||||
if (name == "Ruby") { return 0xE0115F }
|
||||
if (name == "Cardinal") { return 0xC41E3A }
|
||||
if (name == "IndianRed") { return 0xCD5C5C }
|
||||
if (name == "Rust") { return 0xB7410E }
|
||||
if (name == "Sangria") { return 0x92000A }
|
||||
if (name == "Redwood") { return 0xA45A52 }
|
||||
if (name == "Cerise") { return 0xDE3163 }
|
||||
if (name == "Amaranth") { return 0xE52B50 }
|
||||
if (name == "Carmine") { return 0x960018 }
|
||||
if (name == "Chestnut") { return 0x954535 }
|
||||
if (name == "Brick") { return 0xCB4154 }
|
||||
if (name == "TerraCotta") { return 0xE2725B }
|
||||
if (name == "Pink") { return 0xFFC0CB }
|
||||
if (name == "LightPink") { return 0xFFB6C1 }
|
||||
if (name == "HotPink") { return 0xFF69B4 }
|
||||
if (name == "DeepPink") { return 0xFF1493 }
|
||||
if (name == "PaleVioletRed") { return 0xDB7093 }
|
||||
if (name == "Rose") { return 0xFF007F }
|
||||
if (name == "Blush") { return 0xDE5D83 }
|
||||
if (name == "Salmon") { return 0xFA8072 }
|
||||
if (name == "LightSalmon") { return 0xFFA07A }
|
||||
if (name == "DarkSalmon") { return 0xE9967A }
|
||||
if (name == "Coral") { return 0xFF7F50 }
|
||||
if (name == "Watermelon") { return 0xFC6C85 }
|
||||
if (name == "Flamingo") { return 0xFC8EAC }
|
||||
if (name == "Bubblegum") { return 0xFFC1CC }
|
||||
if (name == "Fuchsia") { return 0xFF00FF }
|
||||
if (name == "Magenta") { return 0xFF00FF }
|
||||
if (name == "Mauve") { return 0xE0B0FF }
|
||||
if (name == "Puce") { return 0xCC8899 }
|
||||
if (name == "Thistle") { return 0xD8BFD8 }
|
||||
if (name == "Orchid") { return 0xDA70D6 }
|
||||
if (name == "Orange") { return 0xFFA500 }
|
||||
if (name == "DarkOrange") { return 0xFF8C00 }
|
||||
if (name == "Tangerine") { return 0xF28500 }
|
||||
if (name == "Pumpkin") { return 0xFF7518 }
|
||||
if (name == "Apricot") { return 0xFBCEB1 }
|
||||
if (name == "Peach") { return 0xFFE5B4 }
|
||||
if (name == "Cantaloupe") { return 0xFFA62B }
|
||||
if (name == "Amber") { return 0xFFBF00 }
|
||||
if (name == "Bronze") { return 0xCD7F32 }
|
||||
if (name == "Copper") { return 0xB87333 }
|
||||
if (name == "Marigold") { return 0xEAA221 }
|
||||
if (name == "Carrot") { return 0xED9121 }
|
||||
if (name == "Persimmon") { return 0xEC5800 }
|
||||
if (name == "Papaya") { return 0xFF9E2C }
|
||||
if (name == "Sunset") { return 0xFAD6A5 }
|
||||
if (name == "Yellow") { return 0xFFFF00 }
|
||||
if (name == "LightYellow") { return 0xFFFFE0 }
|
||||
if (name == "Gold") { return 0xFFD700 }
|
||||
if (name == "Goldenrod") { return 0xDAA520 }
|
||||
if (name == "Lemon") { return 0xFFF700 }
|
||||
if (name == "Canary") { return 0xFFEF00 }
|
||||
if (name == "Mustard") { return 0xFFDB58 }
|
||||
if (name == "Flax") { return 0xEEDC82 }
|
||||
if (name == "Wheat") { return 0xF5DEB3 }
|
||||
if (name == "Corn") { return 0xFBEC5D }
|
||||
if (name == "Dandelion") { return 0xF0E130 }
|
||||
if (name == "Saffron") { return 0xF4C430 }
|
||||
if (name == "Khaki") { return 0xF0E68C }
|
||||
if (name == "DarkKhaki") { return 0xBDB76B }
|
||||
if (name == "Straw") { return 0xE4D96F }
|
||||
if (name == "Brown") { return 0x8B4513 }
|
||||
if (name == "SaddleBrown") { return 0x8B4513 }
|
||||
if (name == "Sienna") { return 0xA0522D }
|
||||
if (name == "Chocolate") { return 0xD2691E }
|
||||
if (name == "Peru") { return 0xCD853F }
|
||||
if (name == "Tan") { return 0xD2B48C }
|
||||
if (name == "BurlyWood") { return 0xDEB887 }
|
||||
if (name == "Sand") { return 0xC2B280 }
|
||||
if (name == "Coffee") { return 0x6F4E37 }
|
||||
if (name == "Espresso") { return 0x4B3621 }
|
||||
if (name == "Mahogany") { return 0xC04000 }
|
||||
if (name == "Walnut") { return 0x773F1A }
|
||||
if (name == "Umber") { return 0x635147 }
|
||||
if (name == "Sepia") { return 0x704214 }
|
||||
if (name == "Taupe") { return 0x483C32 }
|
||||
if (name == "Fawn") { return 0xE5AA70 }
|
||||
if (name == "Caramel") { return 0xC68E17 }
|
||||
if (name == "Cocoa") { return 0xD2691E }
|
||||
if (name == "Hazel") { return 0x8E7618 }
|
||||
if (name == "Wenge") { return 0x645452 }
|
||||
if (name == "Green") { return 0x008000 }
|
||||
if (name == "Lime") { return 0x00FF00 }
|
||||
if (name == "LimeGreen") { return 0x32CD32 }
|
||||
if (name == "LawnGreen") { return 0x7CFC00 }
|
||||
if (name == "Chartreuse") { return 0x7FFF00 }
|
||||
if (name == "GreenYellow") { return 0xADFF2F }
|
||||
if (name == "SpringGreen") { return 0x00FF7F }
|
||||
if (name == "MintGreen") { return 0x98FF98 }
|
||||
if (name == "SeaGreen") { return 0x2E8B57 }
|
||||
if (name == "MediumSeaGreen") { return 0x3CB371 }
|
||||
if (name == "ForestGreen") { return 0x228B22 }
|
||||
if (name == "DarkGreen") { return 0x006400 }
|
||||
if (name == "OliveDrab") { return 0x6B8E23 }
|
||||
if (name == "Olive") { return 0x808000 }
|
||||
if (name == "Moss") { return 0x8A9A5B }
|
||||
if (name == "Fern") { return 0x4F7942 }
|
||||
if (name == "Emerald") { return 0x50C878 }
|
||||
if (name == "Jade") { return 0x00A86B }
|
||||
if (name == "Malachite") { return 0x0BDA51 }
|
||||
if (name == "Shamrock") { return 0x009E60 }
|
||||
if (name == "Pistachio") { return 0x93C572 }
|
||||
if (name == "Avocado") { return 0x568203 }
|
||||
if (name == "Pine") { return 0x01796F }
|
||||
if (name == "Sage") { return 0x9CAF88 }
|
||||
if (name == "Kelly") { return 0x4CBB17 }
|
||||
if (name == "Hunter") { return 0x355E3B }
|
||||
if (name == "Basil") { return 0x579229 }
|
||||
if (name == "Clover") { return 0x2E8B57 }
|
||||
if (name == "Juniper") { return 0x6D9A79 }
|
||||
if (name == "Neon") { return 0x39FF14 }
|
||||
if (name == "Cyan") { return 0x00FFFF }
|
||||
if (name == "Aqua") { return 0x00FFFF }
|
||||
if (name == "LightCyan") { return 0xE0FFFF }
|
||||
if (name == "PaleTurquoise") { return 0xAFEEEE }
|
||||
if (name == "Aquamarine") { return 0x7FFFD4 }
|
||||
if (name == "Turquoise") { return 0x40E0D0 }
|
||||
if (name == "MediumTurquoise") { return 0x48D1CC }
|
||||
if (name == "DarkTurquoise") { return 0x00CED1 }
|
||||
if (name == "Teal") { return 0x008080 }
|
||||
if (name == "DarkCyan") { return 0x008B8B }
|
||||
if (name == "CadetBlue") { return 0x5F9EA0 }
|
||||
if (name == "Lagoon") { return 0x018E8E }
|
||||
if (name == "Seafoam") { return 0x93E9BE }
|
||||
if (name == "Cerulean") { return 0x007BA7 }
|
||||
if (name == "SkyBlueLight") { return 0x80DAEB }
|
||||
if (name == "Robin") { return 0x00CCCC }
|
||||
if (name == "Verdigris") { return 0x43B3AE }
|
||||
if (name == "Celadon") { return 0xACE1AF }
|
||||
if (name == "Blue") { return 0x0000FF }
|
||||
if (name == "LightBlue") { return 0xADD8E6 }
|
||||
if (name == "PowderBlue") { return 0xB0E0E6 }
|
||||
if (name == "SkyBlue") { return 0x87CEEB }
|
||||
if (name == "LightSkyBlue") { return 0x87CEFA }
|
||||
if (name == "DeepSkyBlue") { return 0x00BFFF }
|
||||
if (name == "DodgerBlue") { return 0x1E90FF }
|
||||
if (name == "CornflowerBlue") { return 0x6495ED }
|
||||
if (name == "SteelBlue") { return 0x4682B4 }
|
||||
if (name == "RoyalBlue") { return 0x4169E1 }
|
||||
if (name == "MediumBlue") { return 0x0000CD }
|
||||
if (name == "DarkBlue") { return 0x00008B }
|
||||
if (name == "Navy") { return 0x000080 }
|
||||
if (name == "MidnightBlue") { return 0x191970 }
|
||||
if (name == "Cobalt") { return 0x0047AB }
|
||||
if (name == "Sapphire") { return 0x0F52BA }
|
||||
if (name == "Denim") { return 0x1560BD }
|
||||
if (name == "Indigo") { return 0x4B0082 }
|
||||
if (name == "Prussian") { return 0x003153 }
|
||||
if (name == "Ultramarine") { return 0x3F00FF }
|
||||
if (name == "Periwinkle") { return 0xCCCCFF }
|
||||
if (name == "Iris") { return 0x5A4FCF }
|
||||
if (name == "Glaucous") { return 0x6082B6 }
|
||||
if (name == "Zaffre") { return 0x0014A8 }
|
||||
if (name == "Berry") { return 0x2E2D88 }
|
||||
if (name == "Purple") { return 0x800080 }
|
||||
if (name == "Violet") { return 0xEE82EE }
|
||||
if (name == "DarkViolet") { return 0x9400D3 }
|
||||
if (name == "BlueViolet") { return 0x8A2BE2 }
|
||||
if (name == "MediumPurple") { return 0x9370DB }
|
||||
if (name == "Amethyst") { return 0x9966CC }
|
||||
if (name == "Plum") { return 0x8E4585 }
|
||||
if (name == "Eggplant") { return 0x614051 }
|
||||
if (name == "Grape") { return 0x6F2DA8 }
|
||||
if (name == "Wine") { return 0x722F37 }
|
||||
if (name == "Mulberry") { return 0xC54B8C }
|
||||
if (name == "Lilac") { return 0xC8A2C8 }
|
||||
if (name == "Wisteria") { return 0xC9A0DC }
|
||||
if (name == "Heliotrope") { return 0xDF73FF }
|
||||
if (name == "Byzantium") { return 0x702963 }
|
||||
if (name == "Tyrian") { return 0x66023C }
|
||||
if (name == "RebeccaPurple") { return 0x663399 }
|
||||
if (name == "Orchid2") { return 0xAF69EF }
|
||||
return -1
|
||||
}
|
||||
|
||||
|
|
@ -116,7 +116,89 @@ fn emit_bin(e: Node) -> Val {
|
|||
return val(r, "int")
|
||||
}
|
||||
|
||||
# ---- named arguments -------------------------------------------------------
|
||||
# An argument list is either all-positional or all-named. When named, each kid
|
||||
# is an E_FINIT (label -> value); this rewrites e.kids into plain value exprs in
|
||||
# the order the callee declares its parameters, so the rest of emit_call is
|
||||
# oblivious to whether the caller used names.
|
||||
fn args_are_named(e: Node) -> bool {
|
||||
var i = 0
|
||||
while i < len(e.kids) { if e.kids[i].kind == E_FINIT { return true }; i = i + 1 }
|
||||
return false
|
||||
}
|
||||
fn reorder_named(e: Node, labels: []ptr) -> void {
|
||||
if not args_are_named(e) { return }
|
||||
var i = 0
|
||||
while i < len(e.kids) {
|
||||
if e.kids[i].kind != E_FINIT { perr("named and positional arguments cannot be mixed in one call") }
|
||||
i = i + 1
|
||||
}
|
||||
if len(e.kids) != len(labels) { perr("wrong number of arguments") }
|
||||
let out = new []Node
|
||||
var li = 0
|
||||
while li < len(labels) {
|
||||
var found: Node = null
|
||||
var k = 0
|
||||
while k < len(e.kids) { if (e.kids[k].s == labels[li]) { found = e.kids[k] }; k = k + 1 }
|
||||
if (found == null) { perr(`no argument named {labels[li]}`) }
|
||||
push(out, found.a)
|
||||
li = li + 1
|
||||
}
|
||||
e.kids = out
|
||||
}
|
||||
# The parameter labels of a resolved fn/extern, in declaration order.
|
||||
fn param_labels(fn: Node) -> []ptr {
|
||||
let out = new []ptr
|
||||
var i = 0
|
||||
while i < len(fn.kids) { if fn.kids[i].kind == N_PARAM { push(out, fn.kids[i].s) }; i = i + 1 }
|
||||
return out
|
||||
}
|
||||
|
||||
# ---- namespaced builtins: Screen.* / Random.* / Input.* --------------------
|
||||
# The game-facing API reads as `subject.action(...)`. Each method maps to a bare
|
||||
# runtime builtin plus the parameter labels callers may use as named arguments;
|
||||
# after reordering we rewrite the callee to that bare name and fall back into the
|
||||
# ordinary builtin path (which resolves it to its rt_ function).
|
||||
fn emit_ns_call(ns: ptr, meth: ptr, e: Node) -> Val {
|
||||
var bare: ptr = null
|
||||
let labels = new []ptr
|
||||
if (ns == "Screen") {
|
||||
if (meth == "clear") { bare = "clear"; push(labels, "color") }
|
||||
if (meth == "fill_rectangle") { bare = "fill_rect"; push(labels, "x"); push(labels, "y"); push(labels, "width"); push(labels, "height"); push(labels, "color") }
|
||||
if (meth == "draw_rectangle") { bare = "frame_rect"; push(labels, "x"); push(labels, "y"); push(labels, "width"); push(labels, "height"); push(labels, "color") }
|
||||
if (meth == "put_pixel") { bare = "put_px"; push(labels, "x"); push(labels, "y"); push(labels, "color") }
|
||||
if (meth == "draw_text") { bare = "text"; push(labels, "x"); push(labels, "y"); push(labels, "text"); push(labels, "color"); push(labels, "scale") }
|
||||
if (meth == "draw_number") { bare = "text_int"; push(labels, "x"); push(labels, "y"); push(labels, "value"); push(labels, "color"); push(labels, "scale") }
|
||||
if (meth == "show") { bare = "present" }
|
||||
if (meth == "width") { bare = "screen_w" }
|
||||
if (meth == "height") { bare = "screen_h" }
|
||||
if (meth == "status") { bare = "status"; push(labels, "text") }
|
||||
}
|
||||
if (ns == "Map") {
|
||||
if (meth == "size") { bare = "map_size"; push(labels, "width"); push(labels, "height") }
|
||||
if (meth == "row") { bare = "map_row"; push(labels, "y"); push(labels, "cells") }
|
||||
if (meth == "tile") { bare = "tile"; push(labels, "x"); push(labels, "y") }
|
||||
}
|
||||
if (ns == "Random") {
|
||||
if (meth == "range") { bare = "rng_range"; push(labels, "low"); push(labels, "high") }
|
||||
if (meth == "chance") { bare = "rng_chance"; push(labels, "percent") }
|
||||
if (meth == "seed") { bare = "seed"; push(labels, "value") }
|
||||
}
|
||||
if (ns == "Input") {
|
||||
if (meth == "key") { bare = "key" }
|
||||
}
|
||||
if (bare == null) { perr(`unknown builtin {ns}.{meth}`) }
|
||||
reorder_named(e, labels)
|
||||
let id = node(E_ID); id.s = bare; e.a = id
|
||||
return emit_call(e)
|
||||
}
|
||||
|
||||
fn emit_call(e: Node) -> Val {
|
||||
# `Subject.action(...)` — a namespaced builtin (Screen/Random/Input).
|
||||
if e.a.kind == E_MEMBER {
|
||||
if e.a.a.kind == E_ID { return emit_ns_call(e.a.a.s, e.a.s, e) }
|
||||
perr("call target is not a function")
|
||||
}
|
||||
let name = e.a.s
|
||||
if (name == "self") { if nself == 0 { return val("0", "entity") }; return val(emit_bind(`load i32, ptr {self_stk[nself - 1]}`), "entity") }
|
||||
if (name == "key") { return val(emit_bind("load i32, ptr @L_key"), "int") }
|
||||
|
|
@ -253,6 +335,7 @@ fn emit_call(e: Node) -> Val {
|
|||
# extern fn: a direct call to the declared link symbol (no @fn_ prefix)
|
||||
let ext = find_extern(name)
|
||||
if (ext != null) {
|
||||
reorder_named(e, param_labels(ext))
|
||||
let eargs = new []ptr
|
||||
let eatys = new []ptr
|
||||
var ei = 0
|
||||
|
|
@ -280,6 +363,7 @@ fn emit_call(e: Node) -> Val {
|
|||
if (fn2 == null) { perr(`unknown function {name}`) }
|
||||
cname = rtname
|
||||
}
|
||||
reorder_named(e, param_labels(fn2))
|
||||
# evaluate args first (their IR is emitted before the call instruction)
|
||||
let args = new []ptr
|
||||
let atys = new []ptr
|
||||
|
|
@ -332,8 +416,13 @@ fn emit_expr(e: Node) -> Val {
|
|||
perr(`unknown identifier {e.s}`)
|
||||
}
|
||||
if e.kind == E_MEMBER {
|
||||
if e.a.kind == E_ID { # `Enum.Variant` -> its ordinal, a compile-time int
|
||||
let ord = enum_ordinal(e.a.s, e.s)
|
||||
if e.a.kind == E_ID {
|
||||
if (e.a.s == "Color") { # `Color.Name` -> its 0xRRGGBB int, at compile time
|
||||
let cv = color_lookup(e.s)
|
||||
if (cv < 0) { perr(`unknown color Color.{e.s}`) }
|
||||
return val(itoa(cv), "int")
|
||||
}
|
||||
let ord = enum_ordinal(e.a.s, e.s) # `Enum.Variant` -> its ordinal, a compile-time int
|
||||
if ord >= 0 { return val(itoa(ord), "int") }
|
||||
}
|
||||
let bt = static_type(e.a) # `x.field` where field is @Computed -> inline it
|
||||
|
|
|
|||
|
|
@ -1,10 +1,25 @@
|
|||
# emit_machine.ludic — `machine <reg> { state Name = v { .. } }` dispatches on a
|
||||
# register's value; `become Name` stores the target state's value back. Both are
|
||||
# reg()/set_reg() calls, resolved to the runtime like any other builtin.
|
||||
# emit_machine.ludic — `machine <state> { state Name = v { .. } }` dispatches on
|
||||
# an int state store; `become Name` writes the target state's value back. The
|
||||
# store is either a named program-scope `var` (loaded/stored directly) or, for
|
||||
# older code, a register index (reg()/set_reg()).
|
||||
|
||||
# If `a` names a program-scope `var`, return it; else null (a register index).
|
||||
fn machine_var(a: Node) -> Node {
|
||||
if a.kind == E_ID {
|
||||
let g = find_global(a.s)
|
||||
if (g != null) { if g.kind == N_VAR { return g } }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fn emit_machine(st: Node) -> void {
|
||||
let regv = emit_expr(st.a)
|
||||
let s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
|
||||
let gv = machine_var(st.a)
|
||||
var s = ""
|
||||
if (gv != null) { s = emit_bind(`load i32, ptr @g_{st.a.s}`) }
|
||||
else {
|
||||
let regv = emit_expr(st.a)
|
||||
s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
|
||||
}
|
||||
if nmach < len(mach_stk) { mach_stk[nmach] = st } else { push(mach_stk, st) }
|
||||
nmach = nmach + 1
|
||||
let endl = lbl("smend")
|
||||
|
|
@ -37,9 +52,14 @@ fn emit_become(st: Node) -> void {
|
|||
var i = 0
|
||||
while i < len(m.kids) { if (m.kids[i].s == st.s) { target = m.kids[i] }; i = i + 1 }
|
||||
if (target != null) {
|
||||
let regv = emit_expr(m.a)
|
||||
let gv = machine_var(m.a)
|
||||
let sv = emit_expr(target.b)
|
||||
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv.code); emit(")\n")
|
||||
if (gv != null) {
|
||||
emit(" store i32 "); emit(sv.code); emit(", ptr @g_"); emit(m.a.s); emit("\n")
|
||||
} else {
|
||||
let regv = emit_expr(m.a)
|
||||
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv.code); emit(")\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
18268
selfhost/ludicc.seed.ll
18268
selfhost/ludicc.seed.ll
File diff suppressed because it is too large
Load diff
|
|
@ -48,9 +48,22 @@ fn ptype() -> ptr {
|
|||
# ---- expressions -----------------------------------------------------------
|
||||
fn expr() -> Node { return p_or() }
|
||||
|
||||
# Call arguments. Each argument is either positional (`expr`) or named
|
||||
# (`name: expr`) — a named argument is an identifier immediately followed by a
|
||||
# colon, which is unambiguous inside a call. Named args are stored as E_FINIT
|
||||
# (s=label, a=value) and reordered to the callee's parameter order at emit time.
|
||||
fn args_call(call: Node) -> void {
|
||||
eat_op("("); skipnl()
|
||||
while not is_op(")") { push(call.kids, expr()); skipnl(); if is_op(",") { pi = pi + 1; skipnl() } }
|
||||
while not is_op(")") {
|
||||
let t = toks[pi]
|
||||
let nx = toks[pi + 1]
|
||||
if t.kind == TK_ID and nx.kind == TK_OP and (nx.text == ":") {
|
||||
let fi = node(E_FINIT); fi.s = eat_id(); eat_op(":"); skipnl(); fi.a = expr(); push(call.kids, fi)
|
||||
} else {
|
||||
push(call.kids, expr())
|
||||
}
|
||||
skipnl(); if is_op(",") { pi = pi + 1; skipnl() }
|
||||
}
|
||||
eat_op(")")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue