Skip to content

Commit

Permalink
[#201] RASP reimplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed May 12, 2022
1 parent e645bc6 commit 7d5ec6b
Show file tree
Hide file tree
Showing 68 changed files with 1,812 additions and 2,401 deletions.
20 changes: 9 additions & 11 deletions application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of emuStudio.
*
* Copyright (C) 2006-2020 Peter Jakubčo
* Copyright (C) 2006-2022 Peter Jakubčo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -54,17 +54,17 @@ dependencies {
providedRuntime project(":plugins:compiler:as-z80")
providedRuntime project(":plugins:compiler:brainc-brainduck")
providedRuntime project(":plugins:compiler:ramc-ram")
// providedRuntime project(":plugins:compiler:raspc-rasp")
providedRuntime project(":plugins:compiler:raspc-rasp")

providedRuntime project(":plugins:memory:ram-mem")
// providedRuntime project(":plugins:memory:rasp-mem")
providedRuntime project(":plugins:memory:rasp-mem")
providedRuntime project(":plugins:memory:ssem-mem")
providedRuntime project(":plugins:memory:byte-mem")

providedRuntime project(":plugins:cpu:8080-cpu")
providedRuntime project(":plugins:cpu:brainduck-cpu")
providedRuntime project(":plugins:cpu:ram-cpu")
// providedRuntime project(":plugins:cpu:rasp-cpu")
providedRuntime project(":plugins:cpu:rasp-cpu")
providedRuntime project(":plugins:cpu:z80-cpu")
providedRuntime project(":plugins:cpu:ssem-cpu")

Expand Down Expand Up @@ -180,13 +180,13 @@ distributions {
from(output(":plugins:compiler:as-ssem"))
from(output(":plugins:compiler:brainc-brainduck"))
from(output(":plugins:compiler:ramc-ram"))
//from(output(":plugins:compiler:raspc-rasp"))
from(output(":plugins:compiler:raspc-rasp"))
}

into('memory') {
include '*.jar'
from(output(":plugins:memory:ram-mem"))
// from(output(":plugins:memory:rasp-mem"))
from(output(":plugins:memory:rasp-mem"))
from(output(":plugins:memory:ssem-mem"))
from(output(":plugins:memory:byte-mem"))
}
Expand All @@ -196,7 +196,7 @@ distributions {
from(output(":plugins:cpu:8080-cpu"))
from(output(":plugins:cpu:brainduck-cpu"))
from(output(":plugins:cpu:ram-cpu"))
// from(output(":plugins:cpu:rasp-cpu"))
from(output(":plugins:cpu:rasp-cpu"))
from(output(":plugins:cpu:z80-cpu"))
from(output(":plugins:cpu:ssem-cpu"))
}
Expand All @@ -213,16 +213,14 @@ distributions {
}

// Examples
//["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram", "raspc-rasp"]
["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram"].collect { compiler ->
["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram", "raspc-rasp"].collect { compiler ->
from(examples(":plugins:compiler:$compiler")) {
into "examples/$compiler"
}
}

// Scripts
// ["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram", "raspc-rasp"]
["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram"].collect { compiler ->
["as-8080", "as-z80", "as-ssem", "brainc-brainduck", "ramc-ram", "raspc-rasp"].collect { compiler ->
from(scripts(":plugins:compiler:$compiler")) {
into "bin"
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/compiler/ramc-ram/src/main/antlr/RAMParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ rLine:
comment: COMMENT? | COMMENT2?;

rStatement:
instr=rInstruction
| value=rInput
rInstruction
| rInput
;

rInstruction:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import net.emustudio.emulib.plugins.compiler.LexicalAnalyzer;
import net.emustudio.emulib.plugins.compiler.SourceFileExtension;
import net.emustudio.emulib.runtime.*;
import net.emustudio.emulib.runtime.helpers.RadixUtils;
import net.emustudio.plugins.compiler.ram.ast.Program;
import net.emustudio.plugins.compiler.ram.visitors.ProgramParser;
import net.emustudio.plugins.memory.ram.api.RAMMemoryContext;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
Expand All @@ -39,7 +37,7 @@
import java.io.Reader;
import java.util.*;

@PluginRoot(type = PLUGIN_TYPE.COMPILER, title = "RAM Compiler")
@PluginRoot(type = PLUGIN_TYPE.COMPILER, title = "RAM Machine Assembler")
@SuppressWarnings("unused")
public class CompilerRAM extends AbstractCompiler {
private final static Logger LOGGER = LoggerFactory.getLogger(CompilerRAM.class);
Expand All @@ -63,15 +61,17 @@ public String getCopyright() {

@Override
public String getDescription() {
return "RAM machine compiler";
return "RAM machine assembler";
}

public void initialize() {
try {
this.memory = applicationApi.getContextPool().getMemoryContext(pluginID, RAMMemoryContext.class);
} catch (InvalidContextException | ContextNotFoundException e) {
LOGGER.warn("Memory is not available", e);
}
Optional.ofNullable(applicationApi.getContextPool()).ifPresent(pool -> {
try {
memory = pool.getMemoryContext(pluginID, RAMMemoryContext.class);
} catch (InvalidContextException | ContextNotFoundException e) {
LOGGER.warn("Memory is not available", e);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.emustudio.plugins.compiler.ram.visitors;
package net.emustudio.plugins.compiler.ram;

import net.emustudio.plugins.compiler.ram.RAMParser;
import net.emustudio.plugins.compiler.ram.RAMParserBaseVisitor;
import net.emustudio.plugins.compiler.ram.ast.Instruction;
import net.emustudio.plugins.compiler.ram.ast.Label;
import net.emustudio.plugins.compiler.ram.ast.Program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void main(String... args) {
} else {
outputFile = inputFile;
}
outputFile += ".hex";
outputFile += ".bram";
}

CompilerRAM compiler = new CompilerRAM(0L, ApplicationApi.UNAVAILABLE, PluginSettings.UNAVAILABLE);
Expand Down
58 changes: 58 additions & 0 deletions plugins/compiler/raspc-rasp/src/main/antlr/RASPLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
lexer grammar RASPLexer;

COMMENT: ('//' | '--' | ';' | '#' ) ~[\r\n]*;
COMMENT2: '/*' .*? '*/';

fragment A: [aA];
fragment B: [bB];
fragment D: [dD];
fragment E: [eE];
fragment G: [gG];
fragment H: [hH];
fragment I: [iI];
fragment J: [jJ];
fragment L: [lL];
fragment M: [mM];
fragment N: [nN];
fragment O: [oO];
fragment P: [pP];
fragment R: [rR];
fragment S: [sS];
fragment T: [tT];
fragment U: [uU];
fragment V: [vV];
fragment W: [wW];
fragment X: [xX];
fragment Z: [zZ];

OPCODE_READ: R E A D;
OPCODE_WRITE: W R I T E;
OPCODE_LOAD: L O A D;
OPCODE_STORE: S T O R E;
OPCODE_ADD: A D D;
OPCODE_SUB: S U B;
OPCODE_MUL: M U L;
OPCODE_DIV: D I V;
OPCODE_JMP: J M P;
OPCODE_JZ: J Z;
OPCODE_JGTZ: J G T Z;
OPCODE_HALT: H A L T;

OP_CONSTANT: '=';

PREP_ORG: O R G;
PREP_INPUT: '<' I N P U T '>';

LIT_HEXNUMBER_1: '0' X [0-9a-fA-F]+;
LIT_NUMBER: [0-9]+ D?;
LIT_HEXNUMBER_2: [0-9a-fA-F]+ H;
LIT_OCTNUMBER: [0-7]+ [oOqQ];
LIT_BINNUMBER: [01]+ B;

ID_IDENTIFIER: [a-zA-Z_?@] [a-zA-Z_?@0-9]*;
ID_LABEL: ID_IDENTIFIER ':';

WS : [ \t\f]+ -> channel(HIDDEN);
EOL: '\r'? '\n';

ERROR: .;
58 changes: 58 additions & 0 deletions plugins/compiler/raspc-rasp/src/main/antlr/RASPParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
parser grammar RASPParser;

options {
tokenVocab = RASPLexer;
}

rStart:
(rLine EOL rLine)* EOF
| rLine EOF
;

rLine:
label=ID_LABEL? statement=rStatement comment
| label=ID_LABEL comment
| comment
;

comment: COMMENT? | COMMENT2?;

rStatement:
rInstruction
| rInput
| rOrg
;


rInstruction:
op=OPCODE_READ n=rNumber # instrRegister
| op=OPCODE_WRITE OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_WRITE n=rNumber # instrRegister
| op=OPCODE_LOAD OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_LOAD n=rNumber # instrRegister
| op=OPCODE_STORE n=rNumber # instrRegister
| op=OPCODE_ADD OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_ADD n=rNumber # instrRegister
| op=OPCODE_SUB OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_SUB n=rNumber # instrRegister
| op=OPCODE_MUL OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_MUL n=rNumber # instrRegister
| op=OPCODE_DIV OP_CONSTANT n=rNumber # instrConstant
| op=OPCODE_DIV n=rNumber # instrRegister
| op=OPCODE_JMP id=ID_IDENTIFIER # instrJump
| op=OPCODE_JZ id=ID_IDENTIFIER # instrJump
| op=OPCODE_JGTZ id=ID_IDENTIFIER # instrJump
| op=OPCODE_HALT # instrNoOperand
;

rInput: PREP_INPUT rNumber+;

rOrg: PREP_ORG n=rNumber;

rNumber:
n=LIT_HEXNUMBER_1
| n=LIT_NUMBER
| n=LIT_HEXNUMBER_2
| n=LIT_OCTNUMBER
| n=LIT_BINNUMBER
;
Loading

0 comments on commit 7d5ec6b

Please sign in to comment.