• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

修订版3c16c3b79096f4a6c757e06af58bb81d68910725 (tree)
时间2023-01-10 09:15:42
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Use a macro to simplify some append°.

This is a 40% efficient abstraction (5 lines added, 2 lines removed); it
is not worthwhile on its own. It might be more useful in a few minutes,
though.

更改概述

差异

--- a/movelist/cammy-wasm.scm
+++ b/movelist/cammy-wasm.scm
@@ -4,6 +4,16 @@
44
55 (import (cammyo))
66
7+; Flatten an arbitrary number of lists into one list by repeated appending.
8+; This is not as cheap as flatten°, but it correctly preserves nested
9+; structure. It's mostly meant as syntax sugar. Note that the arguments are
10+; backwards from appendo: (concat° out x1 x2 x3 …)
11+(define-syntax concat°
12+ (syntax-rules ()
13+ ((_ xs) (nullo xs))
14+ ((_ xs x) (== xs x))
15+ ((_ xs x1 x2 . rest) (fresh (l) (appendo x1 x2 l) (concat° xs l . rest)))))
16+
717 ; Give a WASM type to a single atomic value.
818 ; Cammy Ty <-> WAT Ty
919 (define (atomic-type° cty wty)
@@ -108,6 +118,11 @@
108118 ((== expr 'succ) (== insts '((i32.const 1) (i32.add))))
109119 ))
110120
121+; Create a fresh symbol ala (gensym s) and bind it to a logic variable.
122+; Meant for creating fresh locals for WASM.
123+; string -> WAT Symbol
124+(define (sym° s v) (conda ((== v (gensym s))) ((symbolo v))))
125+
111126 ; Relate expressions to operations and input+output port contexts.
112127 ; Cammy <-> WAT Insts × Port × Port
113128 (define (ops-ports° expr insts input output)
@@ -120,25 +135,20 @@
120135 (context-prim° expr input output)
121136 (port-ctx° ip _ls1 ictx) (port-ctx° op _ls2 octx)
122137 (get-locals° get ip) (save-locals° save op)
123- (appendo get temp insts) (appendo body save temp)))
138+ (concat° insts get body save)))
124139 ; Composition of stackful portful operations.
125140 ((fresh (f g finsts ginsts ctx)
126141 (== expr `(comp ,f ,g)) (appendo finsts ginsts insts)
127142 (ops-ports° f finsts input ctx) (ops-ports° g ginsts ctx output)))
128143 ))
129144
130-; Create a fresh symbol ala (gensym s) and bind it to a logic variable.
131-; Meant for creating fresh locals for WASM.
132-; string -> WAT Symbol
133-(define (sym° s v) (conda ((== v (gensym s))) ((symbolo v))))
134-
135145 ; Compile code which operates on the WASM stack and also accesses some fresh
136146 ; locals. (WASM toolchains require declaring all locals before usage.)
137147 ; Cammy <-> WAT Insts × WAT Locals
138148 (define (local-ops° expr ops locals)
139149 (conde
140150 ((stack-ops° expr ops) (nullo locals))
141- ((fresh (x f done loop i v t xty xops fops xlocals flocals xflocals block loop1 loop2)
151+ ((fresh (x f done loop i v t xty xops fops xlocals flocals block loopbody)
142152 (== expr `(pr ,x ,f)) (cammy° expr 'N t) (atomic-type° t xty)
143153 (local-ops° x xops xlocals) (local-ops° f fops flocals)
144154 (sym° "$done" done) (sym° "$loop" loop) (sym° "$i" i) (sym° "$v" v)
@@ -146,18 +156,17 @@
146156 (appendo xops
147157 `((local.set ,v) (block ,done ,loop1) (local.get ,v))
148158 block)
149- (appendo
150- `(loop ,loop
151- (local.get ,i) (i32.eqz) (br_if ,done)
152- (local.get ,v))
153- loop2 loop1)
154- (appendo fops
155- `((local.set ,v)
156- (local.get ,i) (i32.const 1) (i32.sub) (local.set ,i)
157- (br ,loop))
158- loop2)
159- (appendo xlocals flocals xflocals)
160- (appendo `((local ,v ,xty) (local ,i i32)) xflocals locals)))
159+ (concat° loopbody
160+ `(loop ,loop
161+ (local.get ,i) (i32.eqz) (br_if ,done)
162+ (local.get ,v))
163+ fops
164+ `((local.set ,v)
165+ (local.get ,i) (i32.const 1) (i32.sub) (local.set ,i)
166+ (br ,loop)))
167+ (concat° locals
168+ `((local ,v ,xty) (local ,i i32)) xlocals flocals)
169+ ))
161170 ))
162171
163172 ; Types for a row of variables on the stack.
@@ -185,15 +194,14 @@
185194 ; Relationally compile to WASM.
186195 ; Cammy <-> string × WAT Func
187196 (define (wasm-func° expr name func)
188- (fresh (source target ops params locals results decls ps rs plen prelude body)
197+ (fresh (source target ops params locals results ps rs plen prelude)
189198 (cammy° expr source target)
190199 (local-ops° expr ops locals)
191200 (row° source params) (row° target results)
192201 (conso 'param params ps) (conso 'result results rs)
193- (appendo `(func ,name ,ps ,rs) locals decls)
194202 ; We have to load each param to the stack at the beginning of the function.
195- (lengtho params plen) (prelude° plen prelude) (appendo prelude ops body)
196- (appendo decls body func)
203+ (lengtho params plen) (prelude° plen prelude)
204+ (concat° func `(func ,name ,ps ,rs) locals prelude ops)
197205 ))
198206
199207 ; The relational boundary for compiling a single function.