Skip to content

Commit 358719b

Browse files
committed
colors: add module; gcode: add user machines; std: improve dicts, add ANSI escape support to strings
1 parent 47fe40c commit 358719b

5 files changed

Lines changed: 63 additions & 11 deletions

File tree

modules/colors.lp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(import "std")
2+
3+
(defn black (s) (concat "\e[0;30m" s "\e[0m"))
4+
(defn red (s) (concat "\e[0;31m" s "\e[0m"))
5+
(defn green (s) (concat "\e[0;32m" s "\e[0m"))
6+
(defn yellow (s) (concat "\e[0;33m" s "\e[0m"))
7+
(defn blue (s) (concat "\e[0;34m" s "\e[0m"))
8+
(defn purple (s) (concat "\e[0;35m" s "\e[0m"))
9+
(defn cyan (s) (concat "\e[0;36m" s "\e[0m"))
10+
(defn white (s) (concat "\e[0;37m" s "\e[0m"))

modules/gcode.lp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
(import "std" "io")
1+
(import "std" "io" "colors")
2+
3+
(set machines $ dict)
4+
5+
(defmacro machine (name init end) (set machines $ insert name '(,init . ,end) machines))
6+
7+
(machine "plra4"
8+
(do (print "T1M6\nG17\n") (set speed 400) (set mat -6.5))
9+
(print "M30"))
210

311
(defn init (name)
412
(eval (set name name) (cond
5-
((== name "plra4") (eval (print "T1M6\nG17\n") (set speed 400) (set mat -6.5)))
6-
(1 (throw $ concat "I don't know this machine: " name)))))
13+
((== (lookup name machines) ()) (throw $ concat "I don't know this machine: " name ".\n" $ blue "Hint: you can use 'machine' to define a machine: (machine name init_func end_func)"))
14+
(1 (do (fst $ lookup name machines))))))
715

816
(defn base () (eval (print "G0Z10\nG0X0Y0\n") (set curx 0) (set cury 0) (set is_cutting ())))
917

@@ -24,7 +32,4 @@
2432
(defn forward (dy) (go curx (+ cury dy)))
2533
(defn backward (dy) (go curx (- cury dy)))
2634

27-
(defn end ()
28-
(cond
29-
((== name "plra4") (print "M30"))
30-
(1 (throw $ concat "I don't know this machine: " name))))
35+
(defn end () (do (snd $ lookup name machines)))

src/Lapse/Modules.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ std =
5959
, ("read", Function lread)
6060
, ("dict", Macros ldict)
6161
, ("lookup", Macros llkp)
62+
, ("insert", Macros lins)
6263
, ("float", Function lflt)
6364
, ("floor", Function lfloor)
65+
, ("throw", Function lthr)
6466
]
6567

6668
io :: Scope
@@ -86,6 +88,7 @@ lapseBuiltins :: Map String String
8688
lapseBuiltins =
8789
fromList
8890
[ ("gcode", BC.unpack $(embedFileRelative "modules/gcode.lp"))
91+
, ("colors", BC.unpack $(embedFileRelative "modules/colors.lp"))
8992
]
9093

9194
fileExists :: FilePath -> IO Bool

src/Lapse/Operators.hs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Lapse.Operators where
66
import Control.Monad.IO.Class (liftIO)
77
import Control.Monad.State (get, lift, put)
88
import Data.Function (fix)
9-
import Data.Map.Strict (Map, empty, insert, (!))
9+
import Data.Map.Strict (Map, empty, insert, (!?))
10+
import Data.Maybe (fromMaybe)
1011
import Lapse.Eval (eval, lmap')
1112
import Lapse.Parser (parse)
1213
import Lapse.Scopes (changeValue, dropScope, newScope)
@@ -237,18 +238,50 @@ ldict (Pair (Pair k (Pair v Nil)) rest) = do
237238
_ -> error "Key should be either string or name"
238239
ldict _ = error "Wrong dict expression"
239240

241+
or' :: String -> String -> String
242+
or' "" a = a
243+
or' a "" = a
244+
or' a _ = a
245+
240246
llkp :: Func
241247
llkp (Pair k' (Pair d' Nil)) = do
242248
a <- eval d'
243249
let d = case a of
244250
Dict x -> x
245251
_ -> error "Wrong lookup expression. Syntax: lookup <key> <dict>"
252+
ek <- eval k'
253+
let sk = case ek of
254+
String x -> x
255+
Name x -> x
256+
_ -> ""
246257
case k' of
247-
String k -> pure $ d ! k
248-
Name k -> pure $ d ! k
258+
String k -> d ?! (sk `or'` k)
259+
Name k -> d ?! (sk `or'` k)
249260
_ -> llkp Nil
261+
where
262+
(?!) d k = pure $ fromMaybe Nil (d !? k)
250263
llkp _ = error "Wrong lookup expression. Syntax: lookup <key> <dict>"
251264

265+
lins :: Func
266+
lins (Pair k' (Pair v' (Pair d' Nil))) = do
267+
a <- eval d'
268+
let d = case a of
269+
Dict x -> x
270+
_ -> error "Wrong insert expression. Syntax: insert <key> <value> <dict>"
271+
v <- eval v'
272+
ek <- eval k'
273+
let sk = case ek of
274+
String x -> x
275+
Name x -> x
276+
_ -> ""
277+
case k' of
278+
String k -> pdict $ insert (sk `or'` k) v d
279+
Name k -> pdict $ insert (sk `or'` k) v d
280+
_ -> lins Nil
281+
where
282+
pdict = pure . Dict
283+
lins _ = error "Wrong insert expression. Syntax: insert <key> <value> <dict>"
284+
252285
lthr :: Func
253-
lthr (Pair (String s) Nil) = error s
286+
lthr (Pair (String s) Nil) = error $ "\ESC[0;31m" ++ s ++ "\ESC[0m"
254287
lthr _ = error "throw syntax: (throw <string>)"

src/Lapse/Parser.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ stringToken (c : cs) cur = case c of
1616
( cur ++ case head cs of
1717
'"' -> ['"']
1818
'n' -> ['\n']
19+
'e' -> ['\ESC']
1920
c' -> '\\' : [c']
2021
)
2122
_ -> stringToken cs (cur ++ [c])

0 commit comments

Comments
 (0)