From 9076115af5c9343e91880576ee513664b63a2300 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Sun, 23 Apr 2017 00:17:08 +0200 Subject: [PATCH] quote and eval, toward macros --- lish/core.lsh | 9 ++++++--- src/Lish/InternalCommands.hs | 23 +++++++++++++++++++++-- src/Lish/Parser.hs | 10 +--------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lish/core.lsh b/lish/core.lsh index 0798b00..9261dbd 100644 --- a/lish/core.lsh +++ b/lish/core.lsh @@ -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) [] diff --git a/src/Lish/InternalCommands.hs b/src/Lish/InternalCommands.hs index 1596017..3b58204 100644 --- a/src/Lish/InternalCommands.hs +++ b/src/Lish/InternalCommands.hs @@ -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) diff --git a/src/Lish/Parser.hs b/src/Lish/Parser.hs index 3ad5a24..66ef9ff 100644 --- a/src/Lish/Parser.hs +++ b/src/Lish/Parser.hs @@ -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