|
| 1 | +import * as t from "https://deno.land/std/testing/asserts.ts"; |
| 2 | +import { Parser } from "./Parser.js"; |
| 3 | + |
| 4 | +const blacketmode = true; |
| 5 | + |
| 6 | +const log = (ast) => { |
| 7 | + console.log(JSON.stringify(ast, null, 2)); |
| 8 | +}; |
| 9 | +const parse = (s) => { |
| 10 | + const p = new Parser(blacketmode); |
| 11 | + return p.parse(s); |
| 12 | +}; |
| 13 | +const expression = (n) => ({ |
| 14 | + expression: { |
| 15 | + arguments: [ |
| 16 | + { |
| 17 | + type: "Literal", |
| 18 | + value: n, |
| 19 | + }, |
| 20 | + ], |
| 21 | + callee: { |
| 22 | + name: "print", |
| 23 | + type: "Identifier", |
| 24 | + }, |
| 25 | + type: "CallExpression", |
| 26 | + }, |
| 27 | + type: "ExpressionStatement", |
| 28 | +}); |
| 29 | +const body = (n) => ({ |
| 30 | + type: "BlockStatement", |
| 31 | + body: [ |
| 32 | + expression(n), |
| 33 | + ] |
| 34 | +}); |
| 35 | + |
| 36 | +Deno.test("if endif", async () => { |
| 37 | + t.assertEquals(parse("if 0 { print 0 }"), { |
| 38 | + body: [ |
| 39 | + { |
| 40 | + type: "IfStatement", |
| 41 | + test: { |
| 42 | + type: "Literal", |
| 43 | + value: 0, |
| 44 | + }, |
| 45 | + consequent: body(0), |
| 46 | + alternate: null, |
| 47 | + }, |
| 48 | + ], |
| 49 | + type: "Program", |
| 50 | + } |
| 51 | + ); |
| 52 | +}); |
| 53 | + |
| 54 | +Deno.test("if else endif", async () => { |
| 55 | + t.assertEquals(parse("if 0 { print 0 } else { print 1 }"), |
| 56 | + { |
| 57 | + body: [ |
| 58 | + { |
| 59 | + type: "IfStatement", |
| 60 | + test: { |
| 61 | + type: "Literal", |
| 62 | + value: 0, |
| 63 | + }, |
| 64 | + consequent: body(0), |
| 65 | + alternate: body(1), |
| 66 | + }, |
| 67 | + ], |
| 68 | + type: "Program", |
| 69 | + } |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +Deno.test("if elseif else endif", async () => { |
| 74 | + const ast = parse("if 0 { print 0 } else if 1 { print 1 } else { print 2 }"); |
| 75 | + //console.log(JSON.stringify(ast, null, 2)); |
| 76 | + t.assertEquals(ast, { |
| 77 | + body: [ |
| 78 | + { |
| 79 | + type: "IfStatement", |
| 80 | + test: { |
| 81 | + type: "Literal", |
| 82 | + value: 0, |
| 83 | + }, |
| 84 | + consequent: body(0), |
| 85 | + alternate: { |
| 86 | + type: "IfStatement", |
| 87 | + test: { |
| 88 | + type: "Literal", |
| 89 | + value: 1, |
| 90 | + }, |
| 91 | + consequent: body(1), |
| 92 | + alternate: body(2), |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + type: "Program", |
| 97 | + } |
| 98 | + ); |
| 99 | +}); |
| 100 | + |
| 101 | +Deno.test("if elseif elseif endif", async () => { |
| 102 | + t.assertEquals(parse(` |
| 103 | + if 0 { |
| 104 | + print 0 |
| 105 | + } else if 1 { |
| 106 | + print 1 |
| 107 | + } else if 1 { |
| 108 | + print 2 |
| 109 | + } else { |
| 110 | + print 3 |
| 111 | + } |
| 112 | + `), { |
| 113 | + body: [ |
| 114 | + { |
| 115 | + "type": "IfStatement", |
| 116 | + "test": { |
| 117 | + "type": "Literal", |
| 118 | + "value": 0 |
| 119 | + }, |
| 120 | + "consequent": body(0), |
| 121 | + "alternate": { |
| 122 | + "type": "IfStatement", |
| 123 | + "test": { |
| 124 | + "type": "Literal", |
| 125 | + "value": 1 |
| 126 | + }, |
| 127 | + "consequent": body(1), |
| 128 | + "alternate": { |
| 129 | + "type": "IfStatement", |
| 130 | + "test": { |
| 131 | + "type": "Literal", |
| 132 | + "value": 1 |
| 133 | + }, |
| 134 | + "consequent": body(2), |
| 135 | + "alternate": body(3), |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + ], |
| 140 | + type: "Program", |
| 141 | + } |
| 142 | + ); |
| 143 | +}); |
| 144 | +/* |
| 145 | +Deno.test("issues#11", async () => { |
| 146 | +const testcode = ` |
| 147 | +if 0 |
| 148 | + if 0 |
| 149 | + print 1 |
| 150 | + elseif 1 |
| 151 | + print 2 |
| 152 | + else |
| 153 | + print 12 |
| 154 | + endif |
| 155 | +else |
| 156 | + print 3 |
| 157 | +endif |
| 158 | +`; |
| 159 | + const ast = parse(testcode); |
| 160 | + //console.log(JSON.stringify(ast, null, 2)); |
| 161 | + t.assert(ast != null); |
| 162 | +}); |
| 163 | +
|
| 164 | +
|
| 165 | +// if 0 |
| 166 | +// if 0 |
| 167 | +// print 1 |
| 168 | +// else |
| 169 | +// if 1 |
| 170 | +// print 2 |
| 171 | +// endif |
| 172 | +// else |
| 173 | +// print 3 |
| 174 | +// endif |
| 175 | +
|
| 176 | +Deno.test("elseif", async () => { |
| 177 | +const testcode = ` |
| 178 | +if 0 |
| 179 | + print 0 |
| 180 | +elseif 1 |
| 181 | + print 1 |
| 182 | +elseif 2 |
| 183 | + print 2 |
| 184 | +endif |
| 185 | +`; |
| 186 | +// if |
| 187 | +// test 0 |
| 188 | +// con print 0 |
| 189 | +// alt if |
| 190 | +// test 1 |
| 191 | +// con print 1 |
| 192 | +// alt if |
| 193 | +// test 2 |
| 194 | +// con print 2 |
| 195 | +// alt null |
| 196 | + const ast = parse(testcode); |
| 197 | + //console.log(JSON.stringify(ast, null, 2)); |
| 198 | + t.assert(ast != null); |
| 199 | +}); |
| 200 | +
|
| 201 | +Deno.test("issues#11 min", async () => { |
| 202 | +const testcode = ` |
| 203 | +if 0 |
| 204 | + if 0 |
| 205 | + print 0 |
| 206 | + elseif 1 |
| 207 | + print 1 |
| 208 | + elseif 2 |
| 209 | + print 2 |
| 210 | + endif |
| 211 | +else |
| 212 | + print 3 |
| 213 | +endif |
| 214 | +`; |
| 215 | +
|
| 216 | + const ast = parse(testcode); |
| 217 | + //console.log(JSON.stringify(ast, null, 2)); |
| 218 | + t.assert(ast != null); |
| 219 | +}); |
| 220 | +
|
| 221 | +
|
| 222 | +Deno.test("issues#11", async () => { |
| 223 | +const testcode = `a=[1,4,2,7,3,8,9,5,6] |
| 224 | +n1=-1 |
| 225 | +n2=-1 |
| 226 | +i = 0 |
| 227 | +if i>0 |
| 228 | + if a[i]>n1 |
| 229 | + n2=n1,n1=a[i] |
| 230 | + elseif a[i]>n2 |
| 231 | + n2=a[i] |
| 232 | + endif |
| 233 | +else |
| 234 | + n1=a[i] |
| 235 | +endif |
| 236 | +print n1,n2 |
| 237 | +`; |
| 238 | + t.assert(parse(testcode) != null); |
| 239 | +}); |
| 240 | +*/ |
| 241 | + |
| 242 | +Deno.test("for if", async () => { |
| 243 | + const ast = parse("for i <- 0 to 2 { if 0 { print 0 } }"); |
| 244 | + //log(ast) |
| 245 | + t.assertEquals(ast, { |
| 246 | + "type": "Program", |
| 247 | + "body": [ |
| 248 | + { |
| 249 | + "type": "ForStatement", |
| 250 | + "init": { |
| 251 | + "type": "AssignmentExpression", |
| 252 | + "operator": "=", |
| 253 | + "left": { |
| 254 | + "type": "Identifier", |
| 255 | + "name": "i" |
| 256 | + }, |
| 257 | + "right": { |
| 258 | + "type": "Literal", |
| 259 | + "value": 0 |
| 260 | + } |
| 261 | + }, |
| 262 | + "test": { |
| 263 | + "type": "BinaryExpression", |
| 264 | + "left": { |
| 265 | + "type": "Identifier", |
| 266 | + "name": "i" |
| 267 | + }, |
| 268 | + "operator": "<=", |
| 269 | + "right": { |
| 270 | + "type": "Literal", |
| 271 | + "value": 2 |
| 272 | + } |
| 273 | + }, |
| 274 | + "update": { |
| 275 | + "type": "AssignmentExpression", |
| 276 | + "operator": "=", |
| 277 | + "left": { |
| 278 | + "type": "Identifier", |
| 279 | + "name": "i" |
| 280 | + }, |
| 281 | + "right": { |
| 282 | + "type": "BinaryExpression", |
| 283 | + "left": { |
| 284 | + "type": "Identifier", |
| 285 | + "name": "i" |
| 286 | + }, |
| 287 | + "operator": "+", |
| 288 | + "right": { |
| 289 | + "type": "Literal", |
| 290 | + "value": 1 |
| 291 | + } |
| 292 | + } |
| 293 | + }, |
| 294 | + "body": { |
| 295 | + "type": "BlockStatement", |
| 296 | + "body": [ |
| 297 | + { |
| 298 | + "type": "IfStatement", |
| 299 | + "test": { |
| 300 | + "type": "Literal", |
| 301 | + "value": 0 |
| 302 | + }, |
| 303 | + "consequent": body(0), |
| 304 | + "alternate": null |
| 305 | + } |
| 306 | + ] |
| 307 | + } |
| 308 | + } |
| 309 | + ] |
| 310 | + }); |
| 311 | +}); |
0 commit comments