Skip to content

Commit

Permalink
NEW ERROR SYSTEM
Browse files Browse the repository at this point in the history
  • Loading branch information
Traineratwot committed May 17, 2024
1 parent 0cbf7ae commit 139f2d2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
},
"dependencies": {
"@types/cypress": "^1.1.3",
"@uiw/codemirror-theme-monokai": "^4.22.0",
"codemirror-lang-ic10": "^0.3.9",
"@uiw/codemirror-theme-monokai": "^4.22.1",
"codemirror-lang-ic10": "^0.3.10",
"cypress": "^13.9.0",
"dayjs": "^1.11.11",
"delay": "^6.0.0",
"driver.js": "^1.3.1",
"ic10": "^4.2.17",
"ic10": "^4.3.1",
"icx-compiler": "^2.1.6",
"is-hotkey": "^0.2.0",
"pako": "^2.1.0",
Expand Down
28 changes: 15 additions & 13 deletions src/components/CodeMirror.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup lang="ts">
import {codeStore} from "../store"
import {Codemirror} from "vue-codemirror"
import {createRuler, hoverOptions, ic10, ic10HoverTooltip, ic10Snippets, lineClassController, zeroLineNumbers} from "codemirror-lang-ic10"
import { codeStore } from "../store"
import { Codemirror } from "vue-codemirror"
import { createRuler, hoverOptions, ic10, ic10HoverTooltip, ic10Snippets, lineClassController, zeroLineNumbers } from "codemirror-lang-ic10"
import interpretator from "../core/ic10.ts"
import {Device, Register} from "ic10/zodTypes"
import {onBeforeUnmount, onMounted, watch} from "vue"
import {monokai} from "@uiw/codemirror-theme-monokai"
import {EditorView} from "codemirror"
import { Device, Register } from "ic10/zodTypes"
import { onBeforeUnmount, onMounted, watch } from "vue"
import { monokai } from "@uiw/codemirror-theme-monokai"
import { EditorView } from "codemirror"
const line = new lineClassController("nextRunLine")
Expand All @@ -19,20 +19,22 @@ onMounted(() => {
watch(
() => codeStore.code,
(newVal) => interpretator.setCode(newVal),
(newVal) => {
interpretator.setCode(newVal)
},
)
onBeforeUnmount(() => {
interpretator.getEnv().off("update")
})
watch(
() => interpretator.getEnv().line,
(newVal) => {
line.highlightLine(newVal + 1)
},
)
onBeforeUnmount(() => {
interpretator.getEnv().off("update")
})
const opt: hoverOptions = {
startLine: 0,
callback: (word: string, text: string | null, line): string | null => {
Expand Down Expand Up @@ -91,7 +93,7 @@ const opt: hoverOptions = {
}
const [ruler] = createRuler(52, "ruler")
const [rulerBeta] = createRuler(90, "ruler")
const extensions = [monokai, EditorView.lineWrapping, zeroLineNumbers, ic10(), ic10Snippets(), ic10HoverTooltip(opt), ruler,rulerBeta, line.extension]
const extensions = [monokai, EditorView.lineWrapping, zeroLineNumbers, ic10(), ic10Snippets(), ic10HoverTooltip(opt), ruler, rulerBeta, line.extension]
</script>

<template>
Expand Down
50 changes: 50 additions & 0 deletions src/core/TokenError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Err, LexerError } from "ic10"

export class TokenError extends Err {
constructor(public error: LexerError) {
//@ts-ignore
super("", "tokenError", error.line.index, error.line.index, error.line.start, error.line.end)
switch (this.error.type) {
case "UNEXPECTED_TOKEN":
if (this.error.expected === undefined) {
this.message = `Unexpected token ${this.error.received}`
break
} else {
if (this.error.expected.length > 1) {
this.message = `Unexpected token ${this.error.received}, expected ${this.error.expected?.join(", ")}`
break
} else {
this.message = `Unexpected token ${this.error.received}, expected ${this.error.expected[0]}`
break
}
}
case "MISSING_TOKEN":
if (this.error.expected === undefined) {
this.message = "Missing token"
break
}
if (this.error.expected.length > 1) {
this.message = `Missing tokens ${this.error.expected?.join(", ")}`
break
} else {
this.message = `Missing token ${this.error.expected[0]}`
break
}
case "UNRECOGNIZED_INSTRUCTION":
const token = this.error.token?.value
if (token === undefined) {
this.message = "You can't use instruction in this place"
} else {
this.message = `You can't use ${this.error.token?.value} instruction in this place`
}
if (this.error.expected) {
this.message += `, expected ${this.error.expected.join(", ")}`
}
break
}

if (this.error.suggested) {
this.message += `, did you mean ${this.error.suggested}?`
}
}
}
12 changes: 10 additions & 2 deletions src/core/ic10.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {InterpreterIc10} from "ic10"
import { findErrorsInCode, InterpreterIc10, LexerError } from "ic10"
import Env from "./Env.ts"
import {reactive, UnwrapNestedRefs} from "vue"
import { reactive, UnwrapNestedRefs } from "vue"
import { TokenError } from "./TokenError.ts"

export class Ic10 extends InterpreterIc10<UnwrapNestedRefs<Env>> {
private static instance: Ic10
Expand All @@ -18,11 +19,18 @@ export class Ic10 extends InterpreterIc10<UnwrapNestedRefs<Env>> {
}

setCode(code: string): this {
this.getEnv().errors = []
this.getEnv().lines = []
super.setCode(code)
this.parseCode()
this.getEnv().prepare()
this.getEnv().emit("update_code")

const erros: LexerError[] = findErrorsInCode(code)
erros.forEach((err) => {
this.getEnv().throw(new TokenError(err))
})

return this
}
getCode() {
Expand Down

0 comments on commit 139f2d2

Please sign in to comment.