From c92e08d0e4958b98d466134ea5912af26856e450 Mon Sep 17 00:00:00 2001 From: Molkars Date: Mon, 21 Aug 2023 09:15:08 -0600 Subject: [PATCH] Add the 'sea' language to the LMSM --- assembly.html | 10 + emulator.html | 1 + firth.html | 1 + functions.html | 1 + index.html | 1 + lmsm-ui-vanilla.js | 88 ++++- lmsm-vanilla.html | 17 +- lmsm.js | 947 ++++++++++++++++++++++++++++++++++++++++++++- sea.html | 556 ++++++++++++++++++++++++++ sea.txt | 72 ++++ 10 files changed, 1666 insertions(+), 28 deletions(-) create mode 100644 sea.html create mode 100644 sea.txt diff --git a/assembly.html b/assembly.html index 3385b02..ac384f7 100644 --- a/assembly.html +++ b/assembly.html @@ -14,6 +14,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

@@ -464,6 +465,15 @@

Stack Instructions

+ + 936 + + + SREM + + + The Stack Remainder Instruction. This instruction will remove the top two values on the top of the value stack and replace them with the remainder of the division of those values + 910 diff --git a/emulator.html b/emulator.html index 2c28f28..d88618c 100644 --- a/emulator.html +++ b/emulator.html @@ -14,6 +14,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/firth.html b/firth.html index b9f9892..d4f9ce1 100644 --- a/firth.html +++ b/firth.html @@ -14,6 +14,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/functions.html b/functions.html index a00fcb0..b7786c7 100644 --- a/functions.html +++ b/functions.html @@ -14,6 +14,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/index.html b/index.html index 249c3f4..d13403c 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/lmsm-ui-vanilla.js b/lmsm-ui-vanilla.js index 3ada004..ff6c1fd 100644 --- a/lmsm-ui-vanilla.js +++ b/lmsm-ui-vanilla.js @@ -1,12 +1,14 @@ class LMSMUi { + static LANGUAGES = ['firth', 'sea'] lmsm = null; + language = null; asmEditor = null; assemblySourceMap = null; - firthEditor = null; - firthSourceMap = null; + codeEditor = null; + codeEditorMap = null; speed = 500; @@ -48,11 +50,12 @@ class LMSMUi { } constructor() { this.lmsm = this.makeLMSM(); + this.language = document.querySelector("#code-language").value; CodeMirror.defineSimpleMode('lmsm-assembly', { start: [ { - regex: /(?:CALL|DAT|ADD|SUB|STA|LDI|LDA|BRA|BRZ|BRP|NOOP|INP|OUT|SPUSH|SPOP|SDUP|SDROP|SSWAP|SADD|SSUB|SMUL|SDIV|SMAX|SMIN|JAL|RET|HLT)\b/, + regex: /(?:CALL|DAT|ADD|SUB|STA|LDI|LDA|BRA|BRZ|BRP|NOOP|INP|OUT|SPUSH|SPOP|SDUP|SDROP|SSWAP|SADD|SSUB|SMUL|SDIV|SMAX|SMIN|JAL|RET|HLT|SLT|SGT|SNOT|SEQ|FST|FLD)\b/, token: 'keyword' }, { @@ -97,15 +100,39 @@ class LMSMUi { ], }); - this.firthEditor = CodeMirror.fromTextArea(document.querySelector('#codeEditorFirth'), { + CodeMirror.defineSimpleMode('sea', { + start: [ + { + regex: /(?:extern|return|void|int|for|while|if|else|break|continue)\b/, + token: 'keyword' + }, + { + regex: /==|!=|=|\+|-|\*|\/|<=|<|>=|>/, + token: 'operator' + }, + { + regex: /\d\d/, + token: 'number', + } + ] + }) + + this.codeEditor = CodeMirror.fromTextArea(document.querySelector('#codeEditorInput'), { lineNumbers: true, tabSize: 2, lint: { getAnnotations: (x) => { let diagnostics = [] - if (this.firthEditor) { - let compiler = new FirthCompiler(); - let compileResult = compiler.compile(this.firthEditor.getValue()); + if (this.codeEditor) { + let compiler; + if (this.language === 'firth') { + compiler = new FirthCompiler(); + } else if (this.language === 'sea') { + compiler = new SeaCompiler(); + } else { + alert("Unknown language: " + this.language) + } + let compileResult = compiler.compile(this.codeEditor.getValue()); for (const error of compileResult.errors) { diagnostics.push({ from: { @@ -125,8 +152,8 @@ class LMSMUi { } }, }); - this.firthEditor.setSize('100%', '25em'); - this.firthEditor.setOption('mode', 'firth'); + this.codeEditor.setSize('100%', '25em'); + this.codeEditor.setOption('mode', this.language); } clearOutput() { @@ -235,6 +262,20 @@ class LMSMUi { detail = "SMAX - The Stack Max Instruction. This instruction will remove the top two values on the top of the value stack and replace them with the maximum of those values"; } else if (935 === nextInstruction) { detail = "SMIN - The Stack Min Instruction. This instruction will remove the top two values on the top of the value stack and replace them with the minimum of those values"; + } else if (936 === nextInstruction) { + detail = "SREM - The Stack Remainder Instruction. This instruction will remove the top two values from the stack and replace them with the remainder of the division of those values"; + } else if (937 === nextInstruction) { + detail = "SEQ - The Stack Equal Instruction. This instruction will remove the top two values from the stack and replace them with 1 if they are equal, and 0 otherwise"; + } else if (939 === nextInstruction) { + detail = "SGT - The Stack Greater Than Instruction. This instruction will remove the top two values from the stack and replace them with 1 if the first is greater than the second, and 0 otherwise"; + } else if (938 === nextInstruction) { + detail = "SLT - The Stack Less Than Instruction. This instruction will remove the top two values on the top of the value stack and replace them with 1 if the first is less than the second, 0 otherwise"; + } else if (940 === nextInstruction) { + detail = "SNOT - The Stack Not Instruction. This instruction will remove the top value on the top of the value stack and replace it with 1 if it is 0, 0 otherwise"; + } else if (941 === nextInstruction) { + detail = "FST - The Stack Store Instruction. This instruction will remove the top value of the value stack and store into the frame-slot in the accumulator"; + } else if (942 === nextInstruction) { + detail = "FLD - The Stack Load Instruction. This instruction will load the value in the memory slot in the accumulator into the accumulator"; } else { detail = "Unknown instruction - The behavior of this machine instruction is undefined."; } @@ -248,11 +289,26 @@ class LMSMUi { return this.lmsm.status; } + setLanguage(language) { + if (!LMSMUi.LANGUAGES.includes(language)) { + alert("Unknown language: " + language); + } + this.language = language; + this.codeEditor.setOption("mode", language); + } + compile() { this.lmsm = this.makeLMSM(); - const code = this.firthEditor.getValue(); + const code = this.codeEditor.getValue(); - let compiler = new FirthCompiler(); + let compiler; + if (this.language === 'firth') { + compiler = new FirthCompiler(); + } else if (this.language === 'sea') { + compiler = new SeaCompiler(); + } else { + alert("Unknown language: " + this.language); + } let compileResult = compiler.compile(code); if (compileResult.errors.length > 0) { console.error("Compilation Errors:") @@ -262,7 +318,7 @@ class LMSMUi { return; } - this.firthSourceMap = compileResult.sourceMap; + this.codeEditorMap = compileResult.sourceMap; this.asmEditor.setValue(compileResult.assembly); let assembler = new LMSMAssembler(); @@ -363,16 +419,16 @@ class LMSMUi { } if (this.lastActiveFirthLine != null) { - this.firthEditor.getDoc() + this.codeEditor.getDoc() .removeLineClass(this.lastActiveFirthLine, 'background', 'markCode'); } - if (this.firthSourceMap != null && this.lastActiveAssemblyLine != null) { - let mappedValue = this.firthSourceMap[this.lastActiveAssemblyLine + 1]; + if (this.codeEditorMap != null && this.lastActiveAssemblyLine != null) { + let mappedValue = this.codeEditorMap[this.lastActiveAssemblyLine + 1]; if (mappedValue != null) { // editor is zero-based this.lastActiveFirthLine = mappedValue - 1; - this.firthEditor.getDoc() + this.codeEditor.getDoc() .addLineClass(this.lastActiveFirthLine, 'background', 'markCode'); } else { this.lastActiveFirthLine = null; diff --git a/lmsm-vanilla.html b/lmsm-vanilla.html index 9f9f6ad..3f61ffd 100644 --- a/lmsm-vanilla.html +++ b/lmsm-vanilla.html @@ -23,10 +23,16 @@ ">
-

🙋 Little Man Stack Machine Emulator

+

🙋 Little Man Stack Machine Emulator

-
- Firth + +
+ + +
- +
-
+ +
LMSM Assembly