Files
rose-ash/lib/go/parse.sx
giles a94ffa0feb
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
go: parse.sx — struct type expressions + 8 tests [proposes-ast]
Adds Go struct types to gp-parse-type:
  struct {}                       →  (list :ty-struct ())
  struct { x int }                →  (list :ty-struct [(:field [x] (:ty-name int))])
  struct { x int; y string }      →  multiple field rows
  struct { x, y int }             →  shared-type row (NAMES is a list)
  struct { inner struct { x int } }  →  nested struct types

gp-parse-struct-fields walks field rows tolerating ASI-inserted semis
(from newlines between fields). Each row collects 1+ names separated
by commas, then a single type that all the names share. Embedded
fields, field tags, and methods are deferred.

The :field shape (NAMES + TYPE) is a recurring multi-language pattern —
struct fields, func params, method receivers, var decls all map to it.
Logged in Blockers as a canonical-AST candidate
(ast-binding-group / ast-named-of-type); worth promoting once a second
consumer (parser of another statically-typed guest, or Go func decls)
exercises the same shape.

parse 98/98, total 227/227.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 08:12:07 +00:00

418 lines
16 KiB
Plaintext

;; lib/go/parse.sx — Go parser. Tokenises via go-tokenize (lib/go/lex.sx),
;; builds canonical AST nodes per lib/guest/ast.sx, and uses
;; pratt-op-lookup from lib/guest/pratt.sx for operator-precedence climbing.
;;
;; Slices so far:
;; 1. Primary expressions — literal / identifier → ast-literal / ast-var
;; 2. Binary operators — Pratt precedence climbing against
;; go-precedence-table; binary application
;; emitted as (ast-app (ast-var OP) [LHS RHS]).
;;
;; The climbing loop is per-language (see lib/guest/pratt.sx header on why)
;; but the entry shape and lookup are shared.
;;
;; All scanner locals are gp- prefixed: SX host primitives silently shadow
;; guest-language defines.
(define
go-precedence-table
(list
(list "*" 5 :left)
(list "/" 5 :left)
(list "%" 5 :left)
(list "<<" 5 :left)
(list ">>" 5 :left)
(list "&" 5 :left)
(list "&^" 5 :left)
(list "+" 4 :left)
(list "-" 4 :left)
(list "|" 4 :left)
(list "^" 4 :left)
(list "==" 3 :left)
(list "!=" 3 :left)
(list "<" 3 :left)
(list "<=" 3 :left)
(list ">" 3 :left)
(list ">=" 3 :left)
(list "&&" 2 :left)
(list "||" 1 :left)))
(define
go-parse
(fn
(src)
(let
((gp-tokens (go-tokenize src)) (gp-idx 0))
(define gp-cur (fn () (nth gp-tokens gp-idx)))
(define gp-advance! (fn () (set! gp-idx (+ gp-idx 1))))
(define gp-tok-type (fn () (get (gp-cur) :type)))
(define gp-tok-value (fn () (get (gp-cur) :value)))
(define
gp-parse-primary
(fn
()
(let
((ty (gp-tok-type)) (v (gp-tok-value)))
(cond
(or
(= ty "int")
(= ty "float")
(= ty "imag")
(= ty "string")
(= ty "rune"))
(do (gp-advance!) (ast-literal v))
(= ty "ident")
(do (gp-advance!) (ast-var v))
:else nil))))
(define
gp-parse-call-args
;; Parse comma-separated args inside (...). Caller has already
;; consumed the opening "(". Consumes the closing ")".
;; Returns a list of argument AST nodes.
(fn
()
(let ((args (list)))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) ")"))
(do (gp-advance!) args)
:else
(do
(let ((first (gp-parse-expr 1)))
(when (not (= first nil)) (append! args first)))
(define
gp-args-rest
(fn
()
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) ","))
(do
(gp-advance!)
(let ((arg (gp-parse-expr 1)))
(when (not (= arg nil)) (append! args arg)))
(gp-args-rest))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ")"))
(gp-advance!)
:else nil)))
(gp-args-rest)
args)))))
(define
gp-parse-struct-fields
;; Caller positioned BEFORE '{'. Parses fields until '}'.
;; field := name [, name]* TYPE
;; Tolerates ASI-inserted semis between fields. Embedded fields
;; (anonymous type without preceding names) and field tags are
;; deferred. Returns a list of (list :field NAMES TYPE).
(fn
()
(when (and (= (gp-tok-type) "op") (= (gp-tok-value) "{"))
(gp-advance!))
(let ((fields (list)))
(define
gp-struct-loop
(fn
()
(cond
(= (gp-tok-type) "semi")
(do (gp-advance!) (gp-struct-loop))
(and (= (gp-tok-type) "op") (= (gp-tok-value) "}"))
(gp-advance!)
(= (gp-tok-type) "ident")
(do
(let ((names (list (gp-tok-value))))
(gp-advance!)
(define
gp-names-rest
(fn
()
(when (and (= (gp-tok-type) "op")
(= (gp-tok-value) ","))
(gp-advance!)
(when (= (gp-tok-type) "ident")
(append! names (gp-tok-value))
(gp-advance!))
(gp-names-rest))))
(gp-names-rest)
(let ((ty (gp-parse-type)))
(append! fields (list :field names ty))))
(gp-struct-loop))
:else nil)))
(gp-struct-loop)
fields)))
(define
gp-parse-func-type-params
;; Anonymous-only func-type params: caller is positioned BEFORE
;; the opening "(". Returns a list of type AST nodes.
;; Named params (a int, b string) are deferred — they're needed
;; for func DECLARATIONS, not pure func-type expressions.
(fn
()
(let ((params (list)))
(when (and (= (gp-tok-type) "op") (= (gp-tok-value) "("))
(gp-advance!))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) ")"))
(do (gp-advance!) params)
:else
(do
(let ((first (gp-parse-type)))
(when (not (= first nil)) (append! params first)))
(define
gp-params-rest
(fn
()
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) ","))
(do
(gp-advance!)
(let ((t (gp-parse-type)))
(when (not (= t nil)) (append! params t)))
(gp-params-rest))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ")"))
(gp-advance!)
:else nil)))
(gp-params-rest)
params)))))
(define
gp-parse-func-type-results
;; Zero, one, or many return types. Caller is positioned after
;; the closing ')' of params.
;; no return — next token is not a type-starter
;; single return — bare type follows
;; multi return — '(' T, T, ... ')'
(fn
()
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "("))
(gp-parse-func-type-params)
:else
(let ((t (gp-parse-type)))
(cond
(= t nil) (list)
:else (list t))))))
(define
gp-parse-type
;; Go type-expression parser. Covers:
;; *T → (list :ty-ptr T)
;; name → (list :ty-name "name")
;; pkg.Name → (list :ty-sel "pkg" "Name")
;; []T → (list :ty-slice T)
;; [N]T → (list :ty-array N T)
;; map[K]V → (list :ty-map K V)
;; chan T → (list :ty-chan :both T)
;; chan<- T → (list :ty-chan :send T)
;; <-chan T → (list :ty-chan :recv T)
;; Struct, interface, func types are deferred to a later slice.
(fn
()
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "*"))
(do (gp-advance!) (list :ty-ptr (gp-parse-type)))
(and (= (gp-tok-type) "op") (= (gp-tok-value) "["))
(do
(gp-advance!)
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(do (gp-advance!) (list :ty-slice (gp-parse-type)))
:else
(let ((sz (gp-parse-expr 1)))
(when (and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(gp-advance!))
(list :ty-array sz (gp-parse-type)))))
(and (= (gp-tok-type) "keyword") (= (gp-tok-value) "map"))
(do
(gp-advance!)
(when (and (= (gp-tok-type) "op") (= (gp-tok-value) "["))
(gp-advance!))
(let ((k (gp-parse-type)))
(when (and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(gp-advance!))
(let ((v (gp-parse-type)))
(list :ty-map k v))))
(and (= (gp-tok-type) "op") (= (gp-tok-value) "<-"))
(do
(gp-advance!)
(when (and (= (gp-tok-type) "keyword") (= (gp-tok-value) "chan"))
(gp-advance!))
(list :ty-chan :recv (gp-parse-type)))
(and (= (gp-tok-type) "keyword") (= (gp-tok-value) "chan"))
(do
(gp-advance!)
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "<-"))
(do (gp-advance!) (list :ty-chan :send (gp-parse-type)))
:else (list :ty-chan :both (gp-parse-type))))
(and (= (gp-tok-type) "keyword") (= (gp-tok-value) "func"))
(do
(gp-advance!)
(let ((params (gp-parse-func-type-params)))
(let ((results (gp-parse-func-type-results)))
(list :ty-func params results))))
(and (= (gp-tok-type) "keyword") (= (gp-tok-value) "struct"))
(do
(gp-advance!)
(list :ty-struct (gp-parse-struct-fields)))
(= (gp-tok-type) "ident")
(let ((name (gp-tok-value)))
(gp-advance!)
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "."))
(do
(gp-advance!)
(cond
(= (gp-tok-type) "ident")
(let ((sel-name (gp-tok-value)))
(gp-advance!)
(list :ty-sel name sel-name))
:else (list :ty-name name)))
:else (list :ty-name name)))
:else nil)))
(define
gp-parse-bracket-expr
;; Optional expression inside brackets — returns nil if next token
;; is ':' or ']' (the slice "omitted" cases).
(fn
()
(cond
(and (= (gp-tok-type) "op")
(or (= (gp-tok-value) ":") (= (gp-tok-value) "]")))
nil
:else (gp-parse-expr 1))))
(define
gp-parse-bracket
;; Caller has consumed '['. Parses index or slice and ']'.
;; x[i] → (list :index BASE i)
;; x[a:b] → (list :slice BASE LOW HIGH nil) (LOW/HIGH may be nil)
;; x[a:b:c] → (list :slice BASE LOW HIGH MAX)
;; Returns the AST node based on BASE.
(fn
(base)
(let ((low (gp-parse-bracket-expr)))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(do (gp-advance!) (list :index base low))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ":"))
(do
(gp-advance!)
(let ((high (gp-parse-bracket-expr)))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(do (gp-advance!) (list :slice base low high nil))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ":"))
(do
(gp-advance!)
(let ((maxe (gp-parse-bracket-expr)))
(when (and (= (gp-tok-type) "op")
(= (gp-tok-value) "]"))
(gp-advance!))
(list :slice base low high maxe)))
:else (list :slice base low high nil))))
:else base))))
(define
gp-parse-postfix
;; Left-associative postfix loop on top of gp-parse-primary:
;; x.field → (list :select x "field") — Go-specific
;; f(args...) → (ast-app f args) — canonical
;; x[i] → (list :index x i) — Go-specific
;; x[a:b] → (list :slice x low high max) — Go-specific
(fn
()
(let ((base (gp-parse-primary)))
(gp-postfix-loop base))))
(define
gp-postfix-loop
(fn
(base)
(cond
(= base nil) nil
:else
(let ((tok (gp-cur)))
(cond
(and (= (get tok :type) "op") (= (get tok :value) "."))
(do
(gp-advance!)
(let ((next-tok (gp-cur)))
(cond
;; .(T) — type assertion
(and (= (get next-tok :type) "op")
(= (get next-tok :value) "("))
(do
(gp-advance!)
(let ((ty (gp-parse-type)))
(when (and (= (gp-tok-type) "op")
(= (gp-tok-value) ")"))
(gp-advance!))
(gp-postfix-loop (list :assert base ty))))
;; .ident — selector / member access
(= (get next-tok :type) "ident")
(do
(gp-advance!)
(gp-postfix-loop
(list :select base (get next-tok :value))))
:else base)))
(and (= (get tok :type) "op") (= (get tok :value) "("))
(do
(gp-advance!)
(gp-postfix-loop (ast-app base (gp-parse-call-args))))
(and (= (get tok :type) "op") (= (get tok :value) "["))
(do
(gp-advance!)
(gp-postfix-loop (gp-parse-bracket base)))
:else base)))))
(define
gp-unary-ops
;; Go spec § Operators: prefix unary, all higher precedence than
;; any binary operator. <- is the channel receive form (send is a
;; statement, not an expression, so never appears here as binary).
(list "+" "-" "!" "^" "*" "&" "<-"))
(define
gp-parse-unary
(fn
()
(let ((tok (gp-cur)))
(cond
(and (= (get tok :type) "op")
(some (fn (u) (= u (get tok :value))) gp-unary-ops))
(do
(gp-advance!)
(let ((operand (gp-parse-unary)))
(cond
(= operand nil) nil
:else (ast-app (ast-var (get tok :value)) (list operand)))))
:else (gp-parse-postfix)))))
(define
gp-parse-expr
(fn
(min-prec)
(let ((left (gp-parse-unary))) (gp-pratt-loop left min-prec))))
(define
gp-pratt-loop
(fn
(left min-prec)
(cond
(= left nil) nil
:else
(let
((tok (gp-cur)))
(cond
(not (= (get tok :type) "op"))
left
:else (let
((entry (pratt-op-lookup go-precedence-table (get tok :value))))
(cond
(= entry nil)
left
(< (pratt-op-prec entry) min-prec)
left
:else (do
(gp-advance!)
(let
((next-min (if (= (pratt-op-assoc entry) :left) (+ (pratt-op-prec entry) 1) (pratt-op-prec entry))))
(let
((right (gp-parse-expr next-min)))
(gp-pratt-loop
(ast-app
(ast-var (get tok :value))
(list left right))
min-prec)))))))))))
(gp-parse-expr 1))))