Open RodrigoDornelles opened 1 year ago
/**
* @file interpreter.c
* @brief Program example for a 3bc syntax interpreter
* using a single virtual machine.
*/
#include "lang_3bc.h"
#include "driver_stack.h"
#include "driver_interrupt.h"
int main(int argc, char** argv)
{
int exitcode = -1;
static uint8_t stack[255];
static uint8_t vm[TBC_MACHINE_SIZE];
driver_stack_init(vm, stack, sizeof(stack));
lang_3bc_cli_init(vm, argc, argv);
while (exitcode == -1) {
lang_3bc_cli_compile(vm);
exitcode = driver_interrupt(vm);
}
return exitcode;
}
#!/usr/bin/env 3bc
MODE 0 6
ALOC 'i' 3
LOOP:
MODE 0 0d02
STRC 0 'H'
STRC 0 'I'
STRC 0 '!'
STRC 0 0xA
MODE 0 0d08
PUSH 'i' 0
MODE 0 0d12
MATH 0 1
MODE 0 0d08
PULL 'i' 0
MODE 0 9
FGTO 0 $LOOP
@startebnf
line = instruction, [comment] | comment | tag;
instruction = register, address, constant;
comment = ("#" | ";"), {?any ASCII character? (* Ignore everything until\nline break or end of file *)}-;
register = mnemonic | number | nill;
address = character | number | hash | nill;
constant = character | number | hash | label | nill;
mnemonic = 4*"a-Z0-9";
number = ?0-9?, {?0-9? | "_"} | "0b", ?0-1?, {?0-1? | "_"} | "0o", ?0-7?, {?0-7? | "_"} | ("0d" | "0i"), ?0-9?, {?0-9? | "_"} | "0x", ?0-9A-F?, {?0-9A-F? | "_"};
nill = "nill";
character = "'", ?any ASCII character? , "'" | "'", "\", ("0" | "t" | "n" | "'" | "\") , "'" ;
hash = ":", { ?non-special ASCII character? }-;
label = "$", { ?non-special ASCII character? }-;
tag = { ?non-special ASCII character? }-, ":";
@endebnf
@startuml
[*] --> TBC_STATE_LANG_READ
TBC_STATE_LANG_READ --> TBC_STATE_LANG_TYPE_TOKEN
TBC_STATE_LANG_TYPE_TOKEN --> TBC_STATE_LANG_LABEL_SCAN
TBC_STATE_LANG_TYPE_TOKEN --> TBC_STATE_LANG_COMMENT
TBC_STATE_LANG_COMMENT --> TBC_STATE_LANG_COMMENT
TBC_STATE_LANG_COMMENT --> TBC_STATE_LANG_READ
TBC_STATE_LANG_LABEL_SCAN --> TBC_STATE_LANG_LABEL_COMMIT
TBC_STATE_LANG_LABEL_COMMIT --> TBC_STATE_LANG_READ
TBC_STATE_LANG_LABEL_COMMIT --> TBC_STATE_LANG_LINE_COMMIT
TBC_STATE_LANG_TYPE_TOKEN --> TBC_STATE_LANG_PARSE_RX
TBC_STATE_LANG_PARSE_RX --> TBC_STATE_LANG_PARSE_RY
TBC_STATE_LANG_PARSE_RY --> TBC_STATE_LANG_PARSE_RZ
TBC_STATE_LANG_PARSE_RZ --> TBC_STATE_LANG_LABEL_COMMIT
TBC_STATE_LANG_LINE_COMMIT --> TBC_STATE_LANG_LINE_COMMIT
TBC_STATE_LANG_LINE_COMMIT --> [*]
@enduml
/**
* @file interpreter.c
* @brief Program example for a 3bc syntax interpreter
* using a single virtual machine.
*/
#include "lang/lang_3bc_cli.h"
#include "lang/lang_3bc_compile.h"
#include "driver/driver_power.h"
#include "driver/driver_stack.h"
#include "driver/driver_interrupt.h"
#include "ds/ds_ram_array.h"
#include "ds/ds_prog_array.h"
int main(int argc, char** argv)
{
static volatile uint8_t memory[2048];
static uint16_t lenght;
static int status = -1;
lenght += driver_power_init(memory);
lenght += driver_stack_init(memory, &memory[lenght], 255);
lenght += ds_ram_array_init(memory, &memory[lenght], 1024);
lenght += lang_3bc_cli_init(memory, &memory[lenght], argc, argv, 255);
lenght += ds_prog_array_init(memory, &memory[lenght], sizeof(memory) - lenght);
while (status == -1) {
lang_3bc_compile(memory);
status = driver_interrupt(memory);
}
return status;
}
heapless first use flag -m
to allow dinamic memory usage #221
related #408