Skip to content

Commit 139f2d2

Browse files
committed
NEW ERROR SYSTEM
1 parent 0cbf7ae commit 139f2d2

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
},
1515
"dependencies": {
1616
"@types/cypress": "^1.1.3",
17-
"@uiw/codemirror-theme-monokai": "^4.22.0",
18-
"codemirror-lang-ic10": "^0.3.9",
17+
"@uiw/codemirror-theme-monokai": "^4.22.1",
18+
"codemirror-lang-ic10": "^0.3.10",
1919
"cypress": "^13.9.0",
2020
"dayjs": "^1.11.11",
2121
"delay": "^6.0.0",
2222
"driver.js": "^1.3.1",
23-
"ic10": "^4.2.17",
23+
"ic10": "^4.3.1",
2424
"icx-compiler": "^2.1.6",
2525
"is-hotkey": "^0.2.0",
2626
"pako": "^2.1.0",

src/components/CodeMirror.vue

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script setup lang="ts">
2-
import {codeStore} from "../store"
3-
import {Codemirror} from "vue-codemirror"
4-
import {createRuler, hoverOptions, ic10, ic10HoverTooltip, ic10Snippets, lineClassController, zeroLineNumbers} from "codemirror-lang-ic10"
2+
import { codeStore } from "../store"
3+
import { Codemirror } from "vue-codemirror"
4+
import { createRuler, hoverOptions, ic10, ic10HoverTooltip, ic10Snippets, lineClassController, zeroLineNumbers } from "codemirror-lang-ic10"
55
import interpretator from "../core/ic10.ts"
6-
import {Device, Register} from "ic10/zodTypes"
7-
import {onBeforeUnmount, onMounted, watch} from "vue"
8-
import {monokai} from "@uiw/codemirror-theme-monokai"
9-
import {EditorView} from "codemirror"
6+
import { Device, Register } from "ic10/zodTypes"
7+
import { onBeforeUnmount, onMounted, watch } from "vue"
8+
import { monokai } from "@uiw/codemirror-theme-monokai"
9+
import { EditorView } from "codemirror"
1010
1111
const line = new lineClassController("nextRunLine")
1212
@@ -19,20 +19,22 @@ onMounted(() => {
1919
2020
watch(
2121
() => codeStore.code,
22-
(newVal) => interpretator.setCode(newVal),
22+
(newVal) => {
23+
interpretator.setCode(newVal)
24+
},
2325
)
2426
25-
onBeforeUnmount(() => {
26-
interpretator.getEnv().off("update")
27-
})
28-
2927
watch(
3028
() => interpretator.getEnv().line,
3129
(newVal) => {
3230
line.highlightLine(newVal + 1)
3331
},
3432
)
3533
34+
onBeforeUnmount(() => {
35+
interpretator.getEnv().off("update")
36+
})
37+
3638
const opt: hoverOptions = {
3739
startLine: 0,
3840
callback: (word: string, text: string | null, line): string | null => {
@@ -91,7 +93,7 @@ const opt: hoverOptions = {
9193
}
9294
const [ruler] = createRuler(52, "ruler")
9395
const [rulerBeta] = createRuler(90, "ruler")
94-
const extensions = [monokai, EditorView.lineWrapping, zeroLineNumbers, ic10(), ic10Snippets(), ic10HoverTooltip(opt), ruler,rulerBeta, line.extension]
96+
const extensions = [monokai, EditorView.lineWrapping, zeroLineNumbers, ic10(), ic10Snippets(), ic10HoverTooltip(opt), ruler, rulerBeta, line.extension]
9597
</script>
9698

9799
<template>

src/core/TokenError.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Err, LexerError } from "ic10"
2+
3+
export class TokenError extends Err {
4+
constructor(public error: LexerError) {
5+
//@ts-ignore
6+
super("", "tokenError", error.line.index, error.line.index, error.line.start, error.line.end)
7+
switch (this.error.type) {
8+
case "UNEXPECTED_TOKEN":
9+
if (this.error.expected === undefined) {
10+
this.message = `Unexpected token ${this.error.received}`
11+
break
12+
} else {
13+
if (this.error.expected.length > 1) {
14+
this.message = `Unexpected token ${this.error.received}, expected ${this.error.expected?.join(", ")}`
15+
break
16+
} else {
17+
this.message = `Unexpected token ${this.error.received}, expected ${this.error.expected[0]}`
18+
break
19+
}
20+
}
21+
case "MISSING_TOKEN":
22+
if (this.error.expected === undefined) {
23+
this.message = "Missing token"
24+
break
25+
}
26+
if (this.error.expected.length > 1) {
27+
this.message = `Missing tokens ${this.error.expected?.join(", ")}`
28+
break
29+
} else {
30+
this.message = `Missing token ${this.error.expected[0]}`
31+
break
32+
}
33+
case "UNRECOGNIZED_INSTRUCTION":
34+
const token = this.error.token?.value
35+
if (token === undefined) {
36+
this.message = "You can't use instruction in this place"
37+
} else {
38+
this.message = `You can't use ${this.error.token?.value} instruction in this place`
39+
}
40+
if (this.error.expected) {
41+
this.message += `, expected ${this.error.expected.join(", ")}`
42+
}
43+
break
44+
}
45+
46+
if (this.error.suggested) {
47+
this.message += `, did you mean ${this.error.suggested}?`
48+
}
49+
}
50+
}

src/core/ic10.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {InterpreterIc10} from "ic10"
1+
import { findErrorsInCode, InterpreterIc10, LexerError } from "ic10"
22
import Env from "./Env.ts"
3-
import {reactive, UnwrapNestedRefs} from "vue"
3+
import { reactive, UnwrapNestedRefs } from "vue"
4+
import { TokenError } from "./TokenError.ts"
45

56
export class Ic10 extends InterpreterIc10<UnwrapNestedRefs<Env>> {
67
private static instance: Ic10
@@ -18,11 +19,18 @@ export class Ic10 extends InterpreterIc10<UnwrapNestedRefs<Env>> {
1819
}
1920

2021
setCode(code: string): this {
22+
this.getEnv().errors = []
2123
this.getEnv().lines = []
2224
super.setCode(code)
2325
this.parseCode()
2426
this.getEnv().prepare()
2527
this.getEnv().emit("update_code")
28+
29+
const erros: LexerError[] = findErrorsInCode(code)
30+
erros.forEach((err) => {
31+
this.getEnv().throw(new TokenError(err))
32+
})
33+
2634
return this
2735
}
2836
getCode() {

0 commit comments

Comments
 (0)