~jpl8/lorthc3

57cf1cb87d7c12f46e3d3764a10f7fe13ae0a998 — jpl 1 year, 3 months ago 4138444
Clean up builtin_words
1 files changed, 43 insertions(+), 11 deletions(-)

M src/compiler.rs
M src/compiler.rs => src/compiler.rs +43 -11
@@ 232,8 232,7 @@ impl ForthCompiler {
        self
    }

    fn builtin_words(&mut self) -> &mut Self {
        // Fetch
    fn build_fetch(&mut self) -> &mut Self {
        self.word(String::from("@"), 0)
            .asm
            .load_register(R0, Self::SP, 0)


@@ 241,8 240,10 @@ impl ForthCompiler {
            .store(R0, Self::SP, 0);
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);
        self
    }

        // Store
    fn build_store(&mut self) -> &mut Self {
        self.word(String::from("!"), 0)
            .asm
            .load_register(R0, Self::SP, 0)


@@ 252,7 253,10 @@ impl ForthCompiler {
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);

        // Add
        self
    }

    fn build_add(&mut self) -> &mut Self {
        self.word(String::from("+"), 0)
            .asm
            .load_register(R0, Self::SP, 0)


@@ 261,8 265,10 @@ impl ForthCompiler {
            .store(R1, Self::SP, 0);
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);
        self
    }

        // Key
    fn build_key(&mut self) -> &mut Self {
        self.word(String::from("key"), 0)
            .get_char()
            .asm


@@ 271,7 277,9 @@ impl ForthCompiler {
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);

        // Emit
        self
    }
    fn build_emit(&mut self) -> &mut Self {
        self.word(String::from("emit"), 0)
            .asm
            .load_register(R0, Self::SP, 0)


@@ 280,12 288,16 @@ impl ForthCompiler {
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);

        // Halt
        self
    }
    fn build_bye(&mut self) -> &mut Self {
        self.word(String::from("bye"), 0).asm.halt();
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);

        // Exit
        self
    }
    fn build_exit(&mut self) -> &mut Self {
        self.word(String::from("exit"), 0)
            .asm
            .label("exit")


@@ 294,7 306,9 @@ impl ForthCompiler {
        let next = self.asm.reference_label("next");
        self.asm.branch_unconditionally(next);

        // ;
        self
    }
    fn build_interpret(&mut self) -> &mut Self {
        self.word(String::from(";"), Self::FLAG_IMMEDIATE)
            .asm
            .and(R1, R1, AndMode::Immediate(0));


@@ 307,7 321,10 @@ impl ForthCompiler {
        let compile = self.asm.reference_label("compile");
        self.asm.branch_unconditionally(compile);

        // :
        self
    }

    fn build_compile(&mut self) -> &mut Self {
        self.word(String::from(":"), 0);
        let token = self.asm.reference_label("token");
        self.asm.subroutine(AsmJsrMode::Addr(token));


@@ 361,7 378,9 @@ impl ForthCompiler {
        let docol = self.asm.reference_label("docol");
        self.asm.fill(docol);

        // Docol
        self
    }
    fn build_docol(&mut self) -> &mut Self {
        self.asm
            .label("docol")
            .store(Self::IP, Self::RP, 1)


@@ 372,6 391,19 @@ impl ForthCompiler {

        self
    }
    fn builtin_words(&mut self) -> &mut Self {
        self.build_fetch()
            .build_store()
            .build_add()
            .build_key()
            .build_emit()
            .build_bye()
            .build_exit()
            .build_interpret()
            .build_compile()
            .build_docol();
        self
    }

    fn builtin_vars(&mut self) -> &mut Self {
        self.var(String::from("latest"), "latest")