quote and eval, toward macros
This commit is contained in:
parent
a0ca892fef
commit
9076115af5
3 changed files with 28 additions and 14 deletions
|
@ -1,11 +1,14 @@
|
|||
;; This is lish core
|
||||
(comment This is lish core)
|
||||
|
||||
(def require (fn [x] (eval (str "(do " (cat x) ")"))))
|
||||
|
||||
;; increment
|
||||
(def inc (fn [x] (+ x 1)))
|
||||
|
||||
;; map
|
||||
(def range (fn [from to]
|
||||
(if (< from to)
|
||||
(cons from (range (inc from) to))
|
||||
[])))
|
||||
|
||||
(def map (fn [f lst]
|
||||
(if (empty? lst)
|
||||
[]
|
||||
|
|
|
@ -138,7 +138,7 @@ fn _ _ = return Void
|
|||
strictCommands :: [(Text,ReduceUnawareCommand)]
|
||||
strictCommands = [ ("prn", prn)
|
||||
, ("pr", pr)
|
||||
, (">", toWaitingStream)
|
||||
, ("<-", toWaitingStream)
|
||||
, ("replace", replace)
|
||||
, ("undef",undef)
|
||||
, ("str",str)
|
||||
|
@ -282,6 +282,22 @@ getenv r (expr:_) = do
|
|||
_ -> evalErr "getenv need on atom or a string as argument"
|
||||
getenv _ _ = evalErr "getenv need on atom or a string as argument"
|
||||
|
||||
comment :: Command
|
||||
comment _ _ = return Void
|
||||
|
||||
quote :: Command
|
||||
quote _ exprs = return (List (map Fix exprs))
|
||||
|
||||
evalList :: Command
|
||||
evalList r (List exprs:[]) = r (Lambda exprs)
|
||||
evalList r (x@(Atom _):[]) = do
|
||||
evaluated <- r x
|
||||
evalList r [evaluated]
|
||||
evalList r (x@(Lambda _):[]) = do
|
||||
evaluated <- r x
|
||||
evalList r [evaluated]
|
||||
evalList _ x = evalErr ("Waiting for a list of exprs got: " <> toS (show x))
|
||||
|
||||
unstrictCommands :: [(Text,InternalCommand)]
|
||||
unstrictCommands = [ ("if", InternalCommand "if" lishIf)
|
||||
, ("def", InternalCommand "def" def)
|
||||
|
@ -289,9 +305,12 @@ unstrictCommands = [ ("if", InternalCommand "if" lishIf)
|
|||
, ("do", InternalCommand "do" doCommand)
|
||||
, ("=", InternalCommand "=" equal)
|
||||
, ("export", InternalCommand "export" export)
|
||||
, ("eval", InternalCommand "eval" evalStr)
|
||||
, ("quote", InternalCommand "quote" quote)
|
||||
, ("eval-str", InternalCommand "eval-str" evalStr)
|
||||
, ("eval", InternalCommand "eval" evalList)
|
||||
, ("getenv", InternalCommand "getenv" getenv)
|
||||
, ("$", InternalCommand "$" getenv)
|
||||
, ("comment", InternalCommand "comment" comment)
|
||||
-- list ops
|
||||
, ("empty?",InternalCommand "empty?" emptyCmd)
|
||||
, ("first",InternalCommand "first" firstCmd)
|
||||
|
|
|
@ -6,7 +6,6 @@ module Lish.Parser
|
|||
where
|
||||
|
||||
import Data.Fix
|
||||
import qualified Data.Text as Text
|
||||
import Protolude hiding (for, many, optional, try, (<|>))
|
||||
import Text.Parsec
|
||||
import Text.Parsec.Text
|
||||
|
@ -14,14 +13,7 @@ import Text.Parsec.Text
|
|||
import Lish.Types
|
||||
|
||||
parseCmd :: Text -> Either ParseError Expr
|
||||
parseCmd = parse parseExpr "S-Expr" . Text.strip . eatComment
|
||||
|
||||
eatComment :: Text -> Text
|
||||
eatComment t =
|
||||
t
|
||||
& Text.lines
|
||||
& map (Text.takeWhile (/= ';'))
|
||||
& Text.intercalate "\n"
|
||||
parseCmd = parse parseExpr "S-Expr"
|
||||
|
||||
parseExpr :: Parser Expr
|
||||
parseExpr = parseLambda
|
||||
|
|
Loading…
Reference in a new issue