2017-07-05 10:15:41 +03:00
|
|
|
Object subclass: Core [
|
|
|
|
Ns := Dictionary new.
|
|
|
|
Core class >> Ns [ ^Ns ]
|
|
|
|
|
|
|
|
Core class >> coerce: block [
|
|
|
|
block value ifTrue: [ ^MALObject True ] ifFalse: [ ^MALObject False ]
|
|
|
|
]
|
|
|
|
|
2017-07-09 11:51:56 +03:00
|
|
|
Core class >> nilable: args else: block [
|
|
|
|
args first type = #nil ifTrue: [
|
|
|
|
^MALObject Nil
|
|
|
|
] ifFalse: [
|
|
|
|
^block value
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2017-07-05 10:15:41 +03:00
|
|
|
Core class >> printedArgs: args readable: readable sep: sep [
|
|
|
|
| items |
|
|
|
|
items := args collect:
|
|
|
|
[ :arg | Printer prStr: arg printReadably: readable ].
|
|
|
|
"NOTE: {} join returns the unchanged array"
|
|
|
|
items isEmpty ifTrue: [ ^'' ] ifFalse: [ ^items join: sep ]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
Core Ns at: #+ put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALNumber new: args first value + args second value ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #- put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALNumber new: args first value - args second value ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #* put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALNumber new: args first value * args second value ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #/ put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALNumber new: args first value // args second value ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
|
|
|
|
Core Ns at: #'pr-str' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALString new: (Core printedArgs: args readable: true
|
|
|
|
sep: ' ') ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #str put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALString new: (Core printedArgs: args readable: false
|
|
|
|
sep: '') ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #prn put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
|
|
|
(Core printedArgs: args readable: true sep: ' ') displayNl.
|
|
|
|
MALObject Nil ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #println put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
|
|
|
(Core printedArgs: args readable: false sep: ' ') displayNl.
|
|
|
|
MALObject Nil ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
|
2017-07-09 21:05:59 +03:00
|
|
|
Core Ns at: #list put:
|
|
|
|
(Fn new: [ :args | MALList new: (OrderedCollection from: args) ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #'list?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #list ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #'empty?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value isEmpty ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #count put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALNumber new: args first value size ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
|
|
|
|
Core Ns at: #= put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first = args second ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
|
|
|
|
Core Ns at: #< put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value < args second value ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #<= put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value <= args second value ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #> put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value > args second value ] ]).
|
2017-07-05 10:15:41 +03:00
|
|
|
Core Ns at: #>= put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value >= args second value ] ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
|
|
|
|
Core Ns at: #'read-string' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Reader readStr: args first value ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
Core Ns at: #slurp put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALString new: (File path: args first value) contents ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #throw put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALCustomError new signal: args first ]).
|
2017-07-09 21:10:16 +03:00
|
|
|
Core Ns at: #readline put:
|
|
|
|
(Fn new: [ :args |
|
|
|
|
| result |
|
|
|
|
result := ReadLine readLine: args first value.
|
|
|
|
result isString ifTrue: [
|
|
|
|
MALString new: result
|
|
|
|
] ifFalse: [
|
|
|
|
MALObject Nil
|
|
|
|
] ]).
|
|
|
|
Core Ns at: #'time-ms' put:
|
|
|
|
(Fn new: [ :args | MALNumber new: Time millisecondClock ]).
|
2017-07-12 00:38:06 +03:00
|
|
|
Core Ns at: #'gst-eval' put:
|
|
|
|
(Fn new: [ :args | (Behavior evaluate: args first value) toMALValue ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
|
|
|
|
Core Ns at: #atom put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALAtom new: args first ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
Core Ns at: #'atom?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #atom ] ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
Core Ns at: #deref put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | args first value ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
Core Ns at: #'reset!' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | args first value: args second. args second ]).
|
2017-07-07 19:36:48 +03:00
|
|
|
Core Ns at: #'swap!' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-07 19:36:48 +03:00
|
|
|
| a f x xs result |
|
|
|
|
a := args first.
|
2017-07-09 21:05:59 +03:00
|
|
|
f := args second fn.
|
2017-07-07 19:36:48 +03:00
|
|
|
x := a value.
|
|
|
|
xs := args allButFirst: 2.
|
|
|
|
result := f value: (xs copyWithFirst: x).
|
|
|
|
a value: result.
|
2017-07-09 21:05:59 +03:00
|
|
|
result ]).
|
2017-07-07 21:36:27 +03:00
|
|
|
|
|
|
|
Core Ns at: #cons put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALList new: (args second value copyWithFirst: args first) ]).
|
2017-07-07 21:36:27 +03:00
|
|
|
Core Ns at: #concat put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALList new: (OrderedCollection join:
|
|
|
|
(args collect: [ :arg | arg value ])) ]).
|
2017-07-08 20:49:09 +03:00
|
|
|
Core Ns at: #nth put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-08 20:49:09 +03:00
|
|
|
| items index |
|
|
|
|
items := args first value.
|
|
|
|
index := args second value + 1.
|
2017-07-09 21:05:59 +03:00
|
|
|
items at: index ifAbsent: [ MALOutOfBounds new signal ] ]).
|
2017-07-08 20:49:09 +03:00
|
|
|
Core Ns at: #first put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core nilable: args else: [
|
|
|
|
args first value at: 1 ifAbsent: [ MALObject Nil ] ] ]).
|
2017-07-08 20:49:09 +03:00
|
|
|
Core Ns at: #rest put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-08 20:49:09 +03:00
|
|
|
| items rest |
|
|
|
|
items := args first value.
|
2017-07-09 11:51:56 +03:00
|
|
|
(args first type = #nil or: [ items isEmpty ]) ifTrue: [
|
2017-07-08 20:49:09 +03:00
|
|
|
rest := {}
|
|
|
|
] ifFalse: [
|
|
|
|
rest := items allButFirst
|
|
|
|
].
|
2017-07-09 21:05:59 +03:00
|
|
|
MALList new: (OrderedCollection from: rest) ]).
|
2017-07-09 21:10:16 +03:00
|
|
|
Core Ns at: #conj put:
|
|
|
|
(Fn new: [ :args |
|
|
|
|
| kind result items |
|
|
|
|
kind := args first type.
|
|
|
|
result := args first value.
|
|
|
|
items := args allButFirst.
|
|
|
|
|
|
|
|
kind = #list ifTrue: [
|
|
|
|
MALList new: (OrderedCollection from: items reverse, result)
|
|
|
|
] ifFalse: [
|
|
|
|
MALVector new: (OrderedCollection from: result, items)
|
|
|
|
] ]).
|
|
|
|
Core Ns at: #seq put:
|
|
|
|
(Fn new: [ :args |
|
|
|
|
| kind storage result |
|
|
|
|
kind := args first type.
|
|
|
|
storage := args first value.
|
|
|
|
Core nilable: args else: [
|
|
|
|
storage isEmpty ifTrue: [
|
|
|
|
MALObject Nil
|
|
|
|
] ifFalse: [
|
|
|
|
kind = #string ifTrue: [
|
|
|
|
result := (OrderedCollection from: storage) collect:
|
|
|
|
[ :char | MALString new: char asString ].
|
|
|
|
MALList new: result
|
|
|
|
] ifFalse: [
|
|
|
|
MALList new: (OrderedCollection from: storage)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
|
|
|
|
Core Ns at: #apply put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-09 11:51:56 +03:00
|
|
|
| f rest result |
|
2017-07-09 21:05:59 +03:00
|
|
|
f := args first fn.
|
2017-07-09 11:51:56 +03:00
|
|
|
args size < 3 ifTrue: [
|
|
|
|
rest := {}
|
|
|
|
] ifFalse: [
|
|
|
|
rest := args copyFrom: 2 to: args size - 1
|
|
|
|
].
|
|
|
|
rest := rest, args last value.
|
2017-07-09 21:05:59 +03:00
|
|
|
f value: rest ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #map put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-09 11:51:56 +03:00
|
|
|
| items f result |
|
2017-07-09 21:05:59 +03:00
|
|
|
f := args first fn.
|
2017-07-09 11:51:56 +03:00
|
|
|
items := args second value.
|
|
|
|
result := items collect: [ :item | f value: {item} ].
|
2017-07-09 21:05:59 +03:00
|
|
|
MALList new: (OrderedCollection from: result) ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
|
2017-07-09 21:10:16 +03:00
|
|
|
Core Ns at: #meta put:
|
|
|
|
(Fn new: [ :args |
|
|
|
|
| meta |
|
|
|
|
meta := args first meta.
|
|
|
|
meta isNil ifTrue: [ MALObject Nil ] ifFalse: [ meta ] ]).
|
|
|
|
Core Ns at: #'with-meta' put:
|
|
|
|
(Fn new: [ :args | args first withMeta: args second ]).
|
|
|
|
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'nil?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #nil ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'true?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #true ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'false?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #false ] ]).
|
2017-10-10 19:29:36 +03:00
|
|
|
Core Ns at: #'number?' put:
|
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #number ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'symbol?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #symbol ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'keyword?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #keyword ] ]).
|
2017-07-09 21:10:16 +03:00
|
|
|
Core Ns at: #'string?' put:
|
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #string ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'vector?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #vector ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'map?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #map ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'sequential?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #list or:
|
|
|
|
[ args first type = #vector ] ] ]).
|
2017-10-10 19:29:36 +03:00
|
|
|
Core Ns at: #'fn?' put:
|
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #fn or:
|
|
|
|
[ args first type = #func and:
|
|
|
|
[ args first isMacro not ] ] ] ]).
|
|
|
|
Core Ns at: #'macro?' put:
|
|
|
|
(Fn new: [ :args | Core coerce: [ args first type = #func and:
|
|
|
|
[ args first isMacro ] ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
|
|
|
|
Core Ns at: #symbol put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALSymbol new: args first value asSymbol ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #keyword put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALKeyword new: args first value asSymbol ]).
|
2020-07-21 19:01:48 +03:00
|
|
|
Core Ns at: #'vec' put:
|
|
|
|
(Fn new: [ :args | MALVector new: args first value ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #vector put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALVector new: (OrderedCollection from: args) ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'hash-map' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALMap new: args asDictionary ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
|
|
|
|
Core Ns at: #assoc put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-09 11:51:56 +03:00
|
|
|
| result keyVals |
|
|
|
|
result := Dictionary from: args first value associations.
|
|
|
|
keyVals := args allButFirst.
|
|
|
|
1 to: keyVals size by: 2 do:
|
|
|
|
[ :i | result add: (keyVals at: i) -> (keyVals at: i + 1) ].
|
2017-07-09 21:05:59 +03:00
|
|
|
MALMap new: result ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #dissoc put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args |
|
2017-07-09 11:51:56 +03:00
|
|
|
| result keys |
|
|
|
|
result := Dictionary from: args first value associations.
|
|
|
|
keys := args allButFirst.
|
|
|
|
keys do: [ :key | result removeKey: key ifAbsent: [ nil ] ].
|
2017-07-09 21:05:59 +03:00
|
|
|
MALMap new: result ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #get put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core nilable: args else:
|
|
|
|
[ args first value at: args second ifAbsent: [ MALObject Nil ] ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #'contains?' put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | Core coerce: [ args first value includesKey: args second ] ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #keys put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALList new: (OrderedCollection from: args first value keys) ]).
|
2017-07-09 11:51:56 +03:00
|
|
|
Core Ns at: #vals put:
|
2017-07-09 21:05:59 +03:00
|
|
|
(Fn new: [ :args | MALList new: (OrderedCollection from: args first value values) ]).
|