aboutsummaryrefslogtreecommitdiff
path: root/isjit.icl
blob: 561d8bd407af39bd31e5d3280f89495718670478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module isjit

import StdEnv

import Data.Either
import Data.Maybe
import System.CommandLine

import Sjit.Compile
import Sjit.Syntax
import Sjit.Run

Start w
# (args,w) = getCommandLine w
# (_,cs) = bootstrap
# (io,w) = stdio w
# (io,w) = case args of
	[prog,file]
		# (_,f,w) = fopen file FReadText w
		# (f,io) = file_loop f io cs
		# (_,w) = fclose f w
		-> (io,w)
	[prog]
		-> (interactive_loop io cs, w)
	[prog:_]
		-> (io <<< "Usage: " <<< prog <<< " [FILE]\n", setReturnCode 1 w)
# (_,w) = fclose io w
= w

interactive_loop :: !*File !CompileState -> *File
interactive_loop io cs
# io = io <<< "> "
# (s,io) = freadline io
# (s,cs) = handle_input s cs
# io = case s of
	Nothing -> io
	Just s  -> io <<< s <<< "\n"
= case cs of
	Nothing -> io
	Just cs -> interactive_loop io cs

file_loop :: !*File !*File !CompileState -> (!*File, !*File)
file_loop f io cs
# (e,f) = fend f
| e = (f,io)
# (s,f) = freadline f
# (s,cs) = handle_input s cs
# io = case s of
	Nothing -> io
	Just s  -> io <<< s <<< "\n"
= case cs of
	Nothing -> (f,io)
	Just cs -> file_loop f io cs

handle_input :: !String !CompileState -> (!Maybe String, !Maybe CompileState)
handle_input s cs
| s == "" = (Just "", Nothing)
| s == "quit\n" = (Nothing, Nothing)
| otherwise = case parse_interactive_line s of
	Left e
		-> (Just e, Just cs)
	Right fun -> case compile fun cs of
		Left e
			-> (Just ("\033[0;31mError:\033[0m " +++ e), Just cs)
		Right cs
			| fun.fun_name <> "main" -> (Nothing, Just cs)
			# res = exec cs
			-> (Just (toString res), Just cs)