X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fmain.icl;h=0dfab03e0a2c3e22ac13f79bcea0edf2737e7480;hb=eb91d7c6b2010be6a43de0a978373654ba3deacc;hp=f2b5c9383be05c655936f41854dda635c10f2b9e;hpb=1ff731746e0ca7be7ae4dc09bb7e86e15b5ce42c;p=cc1516.git diff --git a/src/main.icl b/src/main.icl index f2b5c93..0dfab03 100644 --- a/src/main.icl +++ b/src/main.icl @@ -4,11 +4,15 @@ import StdFile import StdBool import StdMisc import StdFunc +import StdTuple import StdList import StdString import Data.Either import Data.Maybe +import Data.Func import System.CommandLine +import GenPrint +from Text import class Text(concat), instance Text String import parse import lex @@ -20,45 +24,48 @@ import lex fp :: Maybe String, help :: Bool} -:: *StartType :== (LexerOutput, ParserOutput, *World) +derive gPrint TokenValue -Start :: *World -> *StartType +Start :: *World -> *World Start w # (args, w) = parseArgs w | args.help # (out, w) = stdio w - # out = out <<< "\nUsage: " <<< args.program <<< " [opts] [FILENAME]\n" + # out = out <<< "Usage: " <<< args.program <<< " [opts] [FILENAME]\n" <<< "\t--help Show this help\n" - <<< "\t--lex Lex only, is mutually exclusive with --parse\n" - <<< "\t--parse Lex & Parse only\n\n" + <<< "\t--[no-]lex Lexer output(default: disabled)\n" + <<< "\t--[no-]parse Parser output(default: enabled)\n\n" <<< "\tFILENAME File to parse, when unspecified stdin is parsed\n" - # (_, w) = fclose out w - = (Left "", Left "", w) + = snd $ fclose out w # (stdin, w) = stdio w # (contents, stdin, w) = readFileOrStdin stdin args.fp w -| args.lex = case contents of - (Right cs) = (lexer cs, Left "Parsing Disabled", w) - (Left e) = (Left e, Left "Parsing disabled", w) = case contents of - (Left e) = (Left e, Left "", w) + (Left cs) = snd $ fclose (stdin <<< cs) w (Right cs) - # lexout = lexer cs - # parsout = parser lexout - # stdin = stdin <<< (either (const "") toString parsout) - # (_, w) = fclose stdin w - = (lexout, parser lexout, w) + # lexOut = lexer cs + # stdin = if (not args.lex) stdin (case lexOut of + (Left lex) = stdin <<< toString lex + (Right toks) = stdin <<< + concat (map (\(_, _, t)->printToString t +++ "\n") toks)) + # parseOut = parser lexOut + # stdin = if (not args.parse) stdin (case parser lexOut of + (Left parse) = stdin <<< toString parse + (Right ast) = stdin <<< toString ast) + = snd $ fclose stdin w parseArgs :: *World -> (Opts, *World) parseArgs w # ([p:args], w) = getCommandLine w -= (pa args {program=p, lex=False, parse=False, fp=Nothing, help=False}, w) += (pa args {program=p, lex=False, parse=True, fp=Nothing, help=False}, w) where - pa :: [String] -> (Opts -> Opts) - pa [] = id - pa ["--help":r] = \o.pa r {o & help=True} - pa ["--lex":r] = \o.pa r {o & lex=True, parse=False} - pa ["--parse":r] = \o.pa r {o & lex=False, parse=True} - pa [x:r] = \o.pa r {o & fp=Just x} + pa :: [String] Opts -> Opts + pa [] o = o + pa ["--help":r] o = pa r {o & help=True} + pa ["--lex":r] o = pa r {o & lex=True} + pa ["--no-lex":r] o = pa r {o & lex=False} + pa ["--parse":r] o = pa r {o & parse=True} + pa ["--no-parse":r] o = pa r {o & parse=False} + pa [x:r] o = pa r {o & fp=Just x} readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World) readFileOrStdin stdin Nothing w