diff --git a/assembly.html b/assembly.html index 3199a8d..3181aed 100644 --- a/assembly.html +++ b/assembly.html @@ -15,6 +15,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

@@ -465,6 +466,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 3d8b876..63aee39 100644 --- a/emulator.html +++ b/emulator.html @@ -15,6 +15,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/firth.html b/firth.html index 41dd000..aa4f745 100644 --- a/firth.html +++ b/firth.html @@ -15,6 +15,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/functions.html b/functions.html index 066244a..f6ddaca 100644 --- a/functions.html +++ b/functions.html @@ -15,6 +15,7 @@

🙋 The Little Man Stack Machine

Assembly & Execution Functions Firth + Sea Emulator Github

diff --git a/index.html b/index.html index 2d69b8c..0de754a 100644 --- a/index.html +++ b/index.html @@ -15,6 +15,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 a275e63..ee71e83 100644 --- a/lmsm-vanilla.html +++ b/lmsm-vanilla.html @@ -24,10 +24,16 @@ ">
-

🙋 Little Man Stack Machine Emulator

+

🙋 Little Man Stack Machine Emulator

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