Search in sources :

Example 1 with AssemblerLexer

use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.

the class Assembler method assemble.

/**
 * Assembles the given code in to an {@link ExecutableCode}.
 *
 * @param code T code to be assembled.
 *
 * @return The executable code.
 *
 * @throws SyntaxError If the code contains syntax error(s).
 * @throws LabelError  If an undefined label used or duplicate labels exist.
 * @see ExecutableCode
 */
public ExecutableCode assemble(String code) throws SyntaxError, LabelError {
    AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AssemblerParser parser = new AssemblerParser(tokens);
    parser.prog();
    if (parser.getNumberOfSyntaxErrors() > 0) {
        throw new SyntaxError("You have error(s) in your code.");
    }
    ConstantValidator.validate(code);
    ExecutableCode executableCode = linker.link(code);
    loader.load(executableCode);
    return executableCode;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AssemblerLexer(com.kasirgalabs.thumb2.AssemblerLexer) ExecutableCode(com.kasirgalabs.etumulator.lang.Linker.ExecutableCode) AssemblerParser(com.kasirgalabs.thumb2.AssemblerParser)

Example 2 with AssemblerLexer

use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.

the class ConstantValidator method validate.

public static void validate(String code) {
    ConstantValidator constantValidator = new ConstantValidator();
    AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AssemblerParser parser = new AssemblerParser(tokens);
    AssemblerParser.ProgContext program = parser.prog();
    constantValidator.visit(program);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AssemblerLexer(com.kasirgalabs.thumb2.AssemblerLexer) AssemblerParser(com.kasirgalabs.thumb2.AssemblerParser)

Example 3 with AssemblerLexer

use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.

the class Linker method link.

/**
 * Returns an ExecutableCode that can then be run by the processor. If the given code contains
 * data section, linker will only generate memory addresses. Without loading process, referenced
 * data will be unpredictable.
 *
 * @param code The code that will be linked.
 *
 * @return The executable code.
 *
 * @throws LabelError If an undefined label used or duplicate labels exist.
 * @see Loader#load(Linker.ExecutableCode)
 */
public ExecutableCode link(String code) throws LabelError {
    definedBranches.clear();
    definedData.clear();
    addressBook.clear();
    secondPass = false;
    this.code = parseCode(code);
    AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AssemblerParser parser = new AssemblerParser(tokens);
    AssemblerParser.ProgContext program = parser.prog();
    visit(program);
    secondPass = true;
    visit(program);
    List<Data> temp = new ArrayList<>(definedData.size());
    definedData.forEach((label, data) -> {
        temp.add(data);
    });
    return new ExecutableCode(this.code, temp);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ArrayList(java.util.ArrayList) AssemblerLexer(com.kasirgalabs.thumb2.AssemblerLexer) AssemblerParser(com.kasirgalabs.thumb2.AssemblerParser)

Aggregations

AssemblerLexer (com.kasirgalabs.thumb2.AssemblerLexer)3 AssemblerParser (com.kasirgalabs.thumb2.AssemblerParser)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 ExecutableCode (com.kasirgalabs.etumulator.lang.Linker.ExecutableCode)1 ArrayList (java.util.ArrayList)1