go: parse.sx — function calls + member access + 12 tests [consumes-ast proposes-ast]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Adds postfix expression forms per Go spec: f() f(x) f(x, y, z) — function calls x.y x.y.z obj.method(x) — selector / member access gp-parse-postfix sits between gp-parse-unary and gp-parse-primary, so calls and selectors bind tighter than any unary prefix — `-f(x)` parses as `-(f(x))`, not `(-f)(x)`. Postfix is left-associative (`x.y.z` = `(x.y).z`), so the loop iterates rather than recurses on the LHS. AST shapes: Call: (ast-app FN ARGS) — canonical Selector: (list :select OBJ "field") — Go-specific tag The selector shape is a kit gap — lib/guest/ast.sx ships ast-app but no ast-select, despite `obj.field` being universal across Go, Rust, Swift, TS, JS, Python, Ruby, Java, C#. Logged in Blockers; tagging [proposes-ast]. Worth promoting on the next nominally-typed guest. parse 49/49, total 178/178. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -64,6 +64,72 @@
|
||||
(= 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-postfix
|
||||
;; Left-associative postfix loop on top of gp-parse-primary:
|
||||
;; x.field → (list :select x "field") — Go-specific node,
|
||||
;; no kit shape covers selector access
|
||||
;; f(args...) → (ast-app f args) — canonical
|
||||
(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 ((field-tok (gp-cur)))
|
||||
(cond
|
||||
(= (get field-tok :type) "ident")
|
||||
(do
|
||||
(gp-advance!)
|
||||
(gp-postfix-loop
|
||||
(list :select base (get field-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))))
|
||||
:else base)))))
|
||||
(define
|
||||
gp-unary-ops
|
||||
;; Go spec § Operators: prefix unary, all higher precedence than
|
||||
@@ -84,7 +150,7 @@
|
||||
(cond
|
||||
(= operand nil) nil
|
||||
:else (ast-app (ast-var (get tok :value)) (list operand)))))
|
||||
:else (gp-parse-primary)))))
|
||||
:else (gp-parse-postfix)))))
|
||||
(define
|
||||
gp-parse-expr
|
||||
(fn
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"language": "go",
|
||||
"total_pass": 166,
|
||||
"total": 166,
|
||||
"total_pass": 178,
|
||||
"total": 178,
|
||||
"suites": [
|
||||
{"name":"lex","pass":129,"total":129,"status":"ok"},
|
||||
{"name":"parse","pass":37,"total":37,"status":"ok"},
|
||||
{"name":"parse","pass":49,"total":49,"status":"ok"},
|
||||
{"name":"types","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"eval","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"runtime","pass":0,"total":0,"status":"pending"},
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Go-on-SX Scoreboard
|
||||
|
||||
**Total: 166 / 166 tests passing**
|
||||
**Total: 178 / 178 tests passing**
|
||||
|
||||
| | Suite | Pass | Total |
|
||||
|---|---|---|---|
|
||||
| ✅ | lex | 129 | 129 |
|
||||
| ✅ | parse | 37 | 37 |
|
||||
| ✅ | parse | 49 | 49 |
|
||||
| ⬜ | types | 0 | 0 |
|
||||
| ⬜ | eval | 0 | 0 |
|
||||
| ⬜ | runtime | 0 | 0 |
|
||||
|
||||
@@ -172,6 +172,76 @@
|
||||
(ast-var "+")
|
||||
(list (ast-var "a") (ast-app (ast-var "-") (list (ast-var "b"))))))
|
||||
|
||||
(go-parse-test
|
||||
"call: f() (no args)"
|
||||
(go-parse "f()")
|
||||
(ast-app (ast-var "f") (list)))
|
||||
|
||||
(go-parse-test
|
||||
"call: f(x)"
|
||||
(go-parse "f(x)")
|
||||
(ast-app (ast-var "f") (list (ast-var "x"))))
|
||||
|
||||
(go-parse-test
|
||||
"call: f(x, y, z)"
|
||||
(go-parse "f(x, y, z)")
|
||||
(ast-app (ast-var "f") (list (ast-var "x") (ast-var "y") (ast-var "z"))))
|
||||
|
||||
(go-parse-test
|
||||
"call: f(1, 2)"
|
||||
(go-parse "f(1, 2)")
|
||||
(ast-app (ast-var "f") (list (ast-literal "1") (ast-literal "2"))))
|
||||
|
||||
(go-parse-test
|
||||
"call: f(a + b) — arg can be a binary expr"
|
||||
(go-parse "f(a + b)")
|
||||
(ast-app
|
||||
(ast-var "f")
|
||||
(list (ast-app (ast-var "+") (list (ast-var "a") (ast-var "b"))))))
|
||||
|
||||
(go-parse-test
|
||||
"call: f(g(x)) — nested"
|
||||
(go-parse "f(g(x))")
|
||||
(ast-app
|
||||
(ast-var "f")
|
||||
(list (ast-app (ast-var "g") (list (ast-var "x"))))))
|
||||
|
||||
(go-parse-test
|
||||
"select: x.y"
|
||||
(go-parse "x.y")
|
||||
(list :select (ast-var "x") "y"))
|
||||
|
||||
(go-parse-test
|
||||
"select: x.y.z (chained left-assoc)"
|
||||
(go-parse "x.y.z")
|
||||
(list :select (list :select (ast-var "x") "y") "z"))
|
||||
|
||||
(go-parse-test
|
||||
"method: obj.method()"
|
||||
(go-parse "obj.method()")
|
||||
(ast-app (list :select (ast-var "obj") "method") (list)))
|
||||
|
||||
(go-parse-test
|
||||
"method: obj.method(x, y)"
|
||||
(go-parse "obj.method(x, y)")
|
||||
(ast-app
|
||||
(list :select (ast-var "obj") "method")
|
||||
(list (ast-var "x") (ast-var "y"))))
|
||||
|
||||
(go-parse-test
|
||||
"postfix: -f(x) → unary applies after call"
|
||||
(go-parse "-f(x)")
|
||||
(ast-app
|
||||
(ast-var "-")
|
||||
(list (ast-app (ast-var "f") (list (ast-var "x"))))))
|
||||
|
||||
(go-parse-test
|
||||
"postfix: f(x) + 1 → call binds tighter than binary +"
|
||||
(go-parse "f(x) + 1")
|
||||
(ast-app
|
||||
(ast-var "+")
|
||||
(list (ast-app (ast-var "f") (list (ast-var "x"))) (ast-literal "1"))))
|
||||
|
||||
(go-parse-test "non-primary: '+'" (go-parse "+") nil)
|
||||
|
||||
(go-parse-test "non-primary: empty" (go-parse "") nil)
|
||||
|
||||
Reference in New Issue
Block a user