This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
/
preprocessor.monkey
208 lines (151 loc) · 3.89 KB
/
preprocessor.monkey
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
Import parser
Function EvalExpr:Expr( toker:Toker )
Local buf:=New StringStack
While toker.Toke And toker.Toke<>"~n" And toker.TokeType<>TOKE_LINECOMMENT
buf.Push toker.Toke
toker.NextToke
Wend
Local source:=buf.Join( "" )
toker=New Toker( "",source )
Local parser:=New Parser( toker,Null )
Local expr:=parser.ParseExpr().Semant()
Return expr
End
Function EvalBool:Bool( toker:Toker )
Local expr:=EvalExpr( toker )
If Not BoolType( expr.exprType ) expr=expr.Cast( Type.boolType,CAST_EXPLICIT )
If expr.Eval() Return True
Return False
End
Function EvalText:String( toker:Toker )
Local expr:=EvalExpr( toker )
Local val:=expr.Eval()
If StringType( expr.exprType )
Return EvalConfigTags( val )
Endif
If BoolType( expr.exprType )
If val Return "True"
Return "False"
End
Return val
End
Function PreProcess$( path$,modpath$="" )
Local cnest,ifnest,line,source:=New StringStack
Local toker:=New Toker( path,LoadString( path ) )
toker.NextToke
PushEnv GetConfigScope()
SetConfigVar "CD",ExtractDir( RealPath( path ) )
If modpath SetConfigVar "MODPATH",modpath
Repeat
If line
source.Push "~n"
While toker.Toke And toker.Toke<>"~n" And toker.TokeType<>TOKE_LINECOMMENT
toker.NextToke
Wend
If Not toker.Toke Exit
toker.NextToke
Endif
line+=1
_errInfo=toker.Path+"<"+toker.Line+">"
If toker.TokeType=TOKE_SPACE toker.NextToke
If toker.Toke<>"#"
If cnest=ifnest
Local line$
While toker.Toke And toker.Toke<>"~n" And toker.TokeType<>TOKE_LINECOMMENT
Local toke$=toker.Toke
line+=toke
toker.NextToke
Wend
If line source.Push line
Endif
Continue
Endif
Local toke:=toker.NextToke
If toker.TokeType=TOKE_SPACE toke=toker.NextToke
Local stm:=toke.ToLower()
Local ty:=toker.TokeType()
toker.NextToke
If stm="end" Or stm="else"
If toker.TokeType=TOKE_SPACE toker.NextToke
If toker.Toke.ToLower()="if"
toker.NextToke
stm+="if"
Endif
Endif
Select stm
Case "rem"
ifnest+=1
Case "if"
ifnest+=1
If cnest=ifnest-1
If EvalBool( toker ) cnest=ifnest
Endif
Case "else"
If Not ifnest Err "#Else without #If"
If cnest=ifnest
cnest|=$10000
Else If cnest=ifnest-1
cnest=ifnest
Endif
Case "elseif"
If Not ifnest Err "#ElseIf without #If"
If cnest=ifnest
cnest|=$10000
Else If cnest=ifnest-1
If EvalBool( toker ) cnest=ifnest
Endif
Case "end","endif"
If Not ifnest Err "#End without #If or #Rem"
ifnest-=1
If ifnest<(cnest & $ffff) cnest=ifnest
Case "print"
If cnest=ifnest
Print EvalText( toker )
Endif
Case "error"
If cnest=ifnest
Err EvalText( toker )
Endif
Default
If cnest=ifnest
If ty=TOKE_IDENT
If toker.TokeType=TOKE_SPACE toker.NextToke
Local op:=toker.Toke()
Select op
Case "=","+="
Select toke
Case "HOST","LANG","CONFIG","TARGET","SAFEMODE"
Err "App config var '"+toke+"' cannot be modified"
End
toker.NextToke
Select op
Case "="
Local expr:=EvalExpr( toker )
Local val:=expr.Eval()
If Not GetConfigVars().Contains( toke )
If StringType( expr.exprType ) val=EvalConfigTags( val )
SetConfigVar toke,val,expr.exprType
Endif
Case "+="
Local val:=EvalText( toker )
Local var:=GetConfigVar( toke )
If BoolType( GetConfigVarType( toke ) )
If var="1" var="True" Else var="False"
Endif
If var And Not val.StartsWith( ";" ) val=";"+val
SetConfigVar toke,var+val
End
Default
Err "Expecting assignment operator."
End
Else
Err "Unrecognized preprocessor directive '"+toke+"'"
Endif
Endif
End
Forever
RemoveConfigVar "CD"
If modpath RemoveConfigVar "MODPATH"
PopEnv
Return source.Join( "" )
End