1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/gnu-smalltalk/step8_macros.st
Nicolas Boulenguez 033892777a Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL
See issue #587.
* Merge eval-ast and eval into a single conditional.
* Expand macros during the apply phase, removing lots of duplicate
  tests, and increasing the overall consistency by allowing the macro
  to be computed instead of referenced by name (`((defmacro! cond
  (...)))` is currently illegal for example).
* Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the
  MAL environment.
* Remove macroexpand and quasiquoteexpand special forms.
* Use pattern-matching style in process/step*.txt.

Unresolved issues:
c.2: unable to reproduce with gcc 11.12.0.
elm: the directory is unchanged.
groovy: sometimes fail, but not on each rebuild.
nasm: fails some new soft tests, but the issue is unreproducible when
  running the interpreter manually.
objpascal: unreproducible with fpc 3.2.2.
ocaml: unreproducible with 4.11.1.
perl6: unreproducible with rakudo 2021.09.

Unrelated changes:
Reduce diff betweens steps.
Prevent defmacro! from mutating functions: c forth logo miniMAL vb.
dart: fix recent errors and warnings
ocaml: remove metadata from symbols.

Improve the logo implementation.
Encapsulate all representation in types.lg and env.lg, unwrap numbers.
Replace some manual iterations with logo control structures.
Reduce the diff between steps.
Use native iteration in env_get and env_map
Rewrite the reader with less temporary strings.
Reduce the number of temporary lists (for example, reverse iteration
with butlast requires O(n^2) allocations).
It seems possible to remove a few exceptions: GC settings
(Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES
(Makefile.impls) .
2024-08-05 11:40:49 -05:00

262 lines
8.8 KiB
Smalltalk

String extend [
String >> loadRelative [
| scriptPath scriptDirectory |
scriptPath := thisContext currentFileName.
scriptDirectory := FilePath stripFileNameFor: scriptPath.
FileStream fileIn: (FilePath append: self to: scriptDirectory)
]
]
'readline.st' loadRelative.
'util.st' loadRelative.
'types.st' loadRelative.
'reader.st' loadRelative.
'printer.st' loadRelative.
'env.st' loadRelative.
'func.st' loadRelative.
'core.st' loadRelative.
Object subclass: MAL [
MAL class >> READ: input [
^Reader readStr: input
]
MAL class >> evalList: list env: env [
^list collect:
[ :item | self EVAL: item env: env ].
]
MAL class >> starts_with: ast sym: sym [
| a a0 |
ast type = #list ifFalse: [ ^false. ].
a := ast value.
a isEmpty ifTrue: [ ^false. ].
a0 := a first.
^a0 type = #symbol and: [ a0 value = sym ].
]
MAL class >> quasiquote: ast [
| result acc |
(ast type = #symbol or: [ ast type = #map ]) ifTrue: [
result := {MALSymbol new: #quote. ast}.
^MALList new: (OrderedCollection from: result)
].
(ast type = #list or: [ ast type = #vector ]) ifFalse: [
^ast
].
(self starts_with: ast sym: #unquote) ifTrue: [
^ast value second
].
result := {}.
acc := MALList new: (OrderedCollection from: result).
ast value reverseDo: [ : elt |
(self starts_with: elt sym: #'splice-unquote') ifTrue: [
result := {MALSymbol new: #concat. elt value second. acc}
] ifFalse: [
result := {MALSymbol new: #cons. self quasiquote: elt. acc}
].
acc := MALList new: (OrderedCollection from: result)
].
ast type = #vector ifTrue: [
result := {MALSymbol new: #vec. acc}.
acc := MALList new: (OrderedCollection from: result)
].
^acc
]
MAL class >> EVAL: aSexp env: anEnv [
| sexp env ast a0 a0_ a1 a1_ a2 a3 function args |
"NOTE: redefinition of method arguments is not allowed"
sexp := aSexp.
env := anEnv.
[
[ :continue |
a0 := env get: #'DEBUG-EVAL'.
(a0 isNil or: [ a0 type = #false or: [ a0 type = #nil ] ] )
ifFalse: [
('EVAL: ' , (Printer prStr: sexp printReadably: true))
displayNl.
].
sexp type = #symbol ifTrue: [
| key value |
key := sexp value.
value := env get: key.
value isNil ifTrue: [
^MALUnknownSymbol new signal: key
].
^value
].
sexp type = #vector ifTrue: [
^MALVector new: (self evalList: sexp value env: env)
].
sexp type = #map ifTrue: [
^MALMap new: (self evalList: sexp value env: env)
].
sexp type ~= #list ifTrue: [
^sexp
].
sexp value isEmpty ifTrue: [
^sexp
].
ast := sexp value.
a0 := ast first.
a0_ := ast first value.
a0_ = #'def!' ifTrue: [
| result |
a1_ := ast second value.
a2 := ast third.
result := self EVAL: a2 env: env.
env set: a1_ value: result.
^result
].
a0_ = #'defmacro!' ifTrue: [
| result |
a1_ := ast second value.
a2 := ast third.
result := (self EVAL: a2 env: env) deepCopy.
result isMacro: true.
env set: a1_ value: result.
^result
].
a0_ = #'let*' ifTrue: [
| env_ |
env_ := Env new: env.
a1_ := ast second value.
a2 := ast third.
1 to: a1_ size by: 2 do:
[ :i | env_ set: (a1_ at: i) value
value: (self EVAL: (a1_ at: i + 1)
env: env_) ].
env := env_.
sexp := a2.
continue value "TCO"
].
a0_ = #do ifTrue: [
| forms last |
ast size < 2 ifTrue: [
forms := {}.
last := MALObject Nil.
] ifFalse: [
forms := ast copyFrom: 2 to: ast size - 1.
last := ast last.
].
forms do: [ :form | self EVAL: form env: env ].
sexp := last.
continue value "TCO"
].
a0_ = #if ifTrue: [
| condition |
a1 := ast second.
a2 := ast third.
a3 := ast at: 4 ifAbsent: [ MALObject Nil ].
condition := self EVAL: a1 env: env.
(condition type = #false or:
[ condition type = #nil ]) ifTrue: [
sexp := a3
] ifFalse: [
sexp := a2
].
continue value "TCO"
].
a0_ = #quote ifTrue: [
a1 := ast second.
^a1
].
a0_ = #quasiquote ifTrue: [
| result |
a1 := ast second.
sexp := self quasiquote: a1.
continue value "TCO"
].
a0_ = #'fn*' ifTrue: [
| binds env_ fn |
a1_ := ast second value.
binds := a1_ collect: [ :item | item value ].
a2 := ast third.
fn := [ :args |
self EVAL: a2 env:
(Env new: env binds: binds exprs: args) ].
^Func new: a2 params: binds env: env fn: fn
].
function := self EVAL: a0 env: env.
args := ast allButFirst asArray.
(function type = #func and: [ function isMacro ]) ifTrue: [
sexp := function fn value: args.
continue value TCO
].
args := self evalList: args env: env.
function type = #fn ifTrue: [ ^function fn value: args ].
function type = #func ifTrue: [
| env_ |
sexp := function ast.
env_ := Env new: function env binds: function params
exprs: args.
env := env_.
continue value "TCO"
]
] valueWithExit
] repeat.
]
MAL class >> PRINT: sexp [
^Printer prStr: sexp printReadably: true
]
MAL class >> rep: input env: env [
^self PRINT: (self EVAL: (self READ: input) env: env)
]
]
| input historyFile replEnv argv |
historyFile := '.mal_history'.
ReadLine readHistory: historyFile.
replEnv := Env new: nil.
argv := Smalltalk arguments.
argv notEmpty ifTrue: [ argv := argv allButFirst ].
argv := OrderedCollection from: (argv collect: [ :arg | MALString new: arg ]).
Core Ns keysAndValuesDo: [ :op :block | replEnv set: op value: block ].
replEnv set: #eval value: (Fn new: [ :args | MAL EVAL: args first env: replEnv ]).
replEnv set: #'*ARGV*' value: (MALList new: argv).
MAL rep: '(def! not (fn* (a) (if a false true)))' env: replEnv.
MAL rep: '(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))' env: replEnv.
MAL rep: '(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list ''if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons ''cond (rest (rest xs)))))))' env: replEnv.
Smalltalk arguments notEmpty ifTrue: [
MAL rep: '(load-file "', Smalltalk arguments first, '")' env: replEnv
] ifFalse: [
[ input := ReadLine readLine: 'user> '. input isNil ] whileFalse: [
input isEmpty ifFalse: [
ReadLine addHistory: input.
ReadLine writeHistory: historyFile.
[ (MAL rep: input env: replEnv) displayNl ]
on: MALEmptyInput do: [ #return ]
on: MALError do:
[ :err | ('error: ', err messageText) displayNl. #return ].
]
].
'' displayNl.
]