Fixes identifier lexing; support REPL file loading
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,10 +1,7 @@ | ||||
| bin/ | ||||
| data/Purr.sqlite | ||||
| data/encryptionKey | ||||
| /result | ||||
| /config.dhall | ||||
| /Dockerfile | ||||
| /docker-stack.yml | ||||
| .stack-work/ | ||||
| *.swp | ||||
| dist* | ||||
|  | ||||
| @ -17,3 +17,9 @@ evaluateFile filePath = do | ||||
|   case Map.lookup "__result" finalEnv of | ||||
|     Just finalResult -> return finalResult | ||||
|     Nothing -> error "No result found in final environment" | ||||
|  | ||||
| evaluateFileEnv :: FilePath -> IO Env | ||||
| evaluateFileEnv filePath = do | ||||
|   contents <- readFile filePath | ||||
|   let asts = parseTricu contents | ||||
|   pure $ evalTricu library asts | ||||
|  | ||||
							
								
								
									
										11
									
								
								src/Lexer.hs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/Lexer.hs
									
									
									
									
									
								
							| @ -17,7 +17,9 @@ keywordT = string "t" *> notFollowedBy alphaNumChar *> pure LKeywordT | ||||
|  | ||||
| identifier :: Lexer LToken | ||||
| identifier = do | ||||
|   name <- some (letterChar <|> char '_' <|> char '-') | ||||
|   first <- letterChar <|> char '_' | ||||
|   rest <- many (letterChar <|> char '_' <|> char '-' <|> digitChar) | ||||
|   let name = first : rest | ||||
|   if (name == "t" || name == "__result") | ||||
|     then fail "Keywords (`t`, `__result`) cannot be used as an identifier" | ||||
|     else return (LIdentifier name) | ||||
| @ -31,11 +33,8 @@ stringLiteral :: Lexer LToken | ||||
| stringLiteral = do | ||||
|   char '"' | ||||
|   content <- many (noneOf ['"']) | ||||
|   if null content | ||||
|     then fail "Empty string literals are not allowed" | ||||
|     else do | ||||
|       char '"' --" | ||||
|       return (LStringLiteral content) | ||||
|   char '"' --" | ||||
|   return (LStringLiteral content) | ||||
|  | ||||
| assign :: Lexer LToken | ||||
| assign = char '=' *> pure LAssign | ||||
|  | ||||
| @ -38,10 +38,10 @@ evaluateMode = Evaluate | ||||
|       &= name "o" | ||||
|   , form = FSL &= typ "FORM"  | ||||
|       &= help "Optional output form: (fsl|tree|ast|ternary|ascii).\n \ | ||||
|              \ Defaults to fsl."  | ||||
|              \ Defaults to fsl \"Fork Stem Leaf\" form."  | ||||
|       &= name "t" | ||||
|   } | ||||
|   &= help "Evaluate a file and return the result of the expression in the final line" | ||||
|   &= help "Evaluate tricu and return the result of the expression in the final line" | ||||
|   &= explicit | ||||
|   &= name "eval" | ||||
|  | ||||
|  | ||||
							
								
								
									
										10
									
								
								src/REPL.hs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/REPL.hs
									
									
									
									
									
								
							| @ -1,6 +1,7 @@ | ||||
| module REPL where | ||||
|  | ||||
| import Eval | ||||
| import FileEval | ||||
| import Lexer | ||||
| import Parser | ||||
| import Research | ||||
| @ -21,6 +22,15 @@ repl env = runInputT defaultSettings (loop env) | ||||
|       case minput of | ||||
|         Nothing -> outputStrLn "Goodbye!" | ||||
|         Just ":_exit" -> outputStrLn "Goodbye!" | ||||
|         Just ":_load" -> do | ||||
|           path <- getInputLine "File path to load < " | ||||
|           case path of | ||||
|             Nothing -> do | ||||
|               outputStrLn "No input received; stopping import." | ||||
|               loop env | ||||
|             Just path -> do | ||||
|               loadedEnv <- liftIO $ evaluateFileEnv path | ||||
|               loop $ Map.union loadedEnv env | ||||
|         Just "" -> do | ||||
|           outputStrLn "" | ||||
|           loop env | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 James Eversole
					James Eversole