i hate my life
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
But uhh that's the name of the VM itself, the name of the programming language I made is Sylt, named after the German island.
|
||||
|
||||
To compile the VM for Linux: `gcc src/main.c src/vmbl.c src/exception.c src/file_utils.c -o vmbl -O3`
|
||||
To compile SASM for Linux: `gcc src/asm/sasm.c src/asm/tokenize.c src/file_utils.c src/asm/assembler.c -o sasm -O3`
|
||||
To compile SASM for Linux: `gcc src/asm/sasm.c src/asm/instructions.c src/file_utils.c src/asm/assembler.c -o sasm -O3`
|
||||
|
||||
## Syntax
|
||||
### Example "Hello, World!" Program
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
#include "assembler.h"
|
||||
#include "instructions.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
VMBL_Instruction assembleLine(char *line) {
|
||||
char *instName = strtok(line, " ");
|
||||
printf("%s\n", instName);
|
||||
}
|
||||
|
||||
void assemble(char *sourceCode, VMBL_Instruction program[], size_t programCapacity) {
|
||||
char *line = strtok(sourceCode, "\n");
|
||||
|
||||
int i = 0;
|
||||
while (line != NULL)
|
||||
{
|
||||
program[i++] = assembleLine(line);
|
||||
line = strtok(NULL, "\n");
|
||||
}
|
||||
|
||||
void assemble(Tokenizer *tokenizer) {
|
||||
Token token = getCurrentToken(tokenizer);
|
||||
while (token.type != TOKEN_EOF)
|
||||
{
|
||||
//printf("%s: %s - line %d\n", tokenTypeAsCStr(token.type), token.value, token.line);
|
||||
|
||||
|
||||
//free(token.value);
|
||||
token = getCurrentToken(tokenizer);
|
||||
|
||||
|
||||
}
|
||||
free(token.value);
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
#ifndef ASSEMBLER_H
|
||||
#define ASSEMBLER_H
|
||||
|
||||
#include "tokenize.h"
|
||||
#include <stddef.h>
|
||||
#include "../vmbl.h"
|
||||
|
||||
|
||||
VMBL_Instruction assembleLine(char *line);
|
||||
void assemble(char *sourceCode, VMBL_Instruction program[], size_t programCapacity);
|
||||
|
||||
void assemble(Tokenizer *tokenizer);
|
||||
|
||||
#endif // !ASSEMBLER_H
|
||||
|
||||
12
src/asm/instructions.c
Normal file
12
src/asm/instructions.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "instructions.h"
|
||||
#include <string.h>
|
||||
|
||||
InstructionType instructionNameToType(char* instName) {
|
||||
for (size_t i = 0; i < sizeof(instruction_table)/sizeof(instruction_table[0]); i++) {
|
||||
|
||||
if (strcmp(instName, instruction_table[i].mnemonic) == 0) {
|
||||
return (InstructionType)i;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,35 @@
|
||||
#ifndef INSTRUCTIONS_H
|
||||
#define INSTRUCTIONS_H
|
||||
|
||||
#include "../vmbl.h"
|
||||
|
||||
#define MAX_ARGS 3
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ARG_TYPE_NONE,
|
||||
ARG_TYPE_INT
|
||||
} ArgType;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *mnemonic,
|
||||
uint8_t argCount,
|
||||
ArgType args[MAX_ARGS]
|
||||
char *mnemonic;
|
||||
uint8_t argCount;
|
||||
ArgType args[MAX_ARGS];
|
||||
} InstructionInfo;
|
||||
|
||||
static const InstructionInfo instruction_table[] = {
|
||||
[INSTRUCTION_NOP] = { "nop", 0, { ARG_TYPE_NONE } },
|
||||
[INSTRUCTION_PUSH] = { "push", 1, { ARG_TYPE_INT } },
|
||||
[INSTRUCTION_DROP] = { "drop", 1, { ARG_TYPE_INT } },
|
||||
[INSTRUCTION_ADD] = { "add", 0, { ARG_TYPE_NONE } },
|
||||
[INSTRUCTION_SUB] = { "sub", 0, { ARG_TYPE_NONE } },
|
||||
[INSTRUCTION_MUL] = { "mul", 0, { ARG_TYPE_NONE } },
|
||||
[INSTRUCTION_DIV] = { "div", 0, { ARG_TYPE_NONE } },
|
||||
[INSTRUCTION_DUPLICATE] = { "dup", 1, { ARG_TYPE_INT } },
|
||||
[INSTRUCTION_HALT] = { "halt", 0, { ARG_TYPE_NONE } },
|
||||
};
|
||||
|
||||
InstructionType instructionNameToType(char* instName);
|
||||
|
||||
#endif // !INSTRUCTIONS_H
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "tokenize.h"
|
||||
#include "../file_utils.h"
|
||||
#include "assembler.h"
|
||||
#include "../vmbl.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
@@ -13,14 +13,16 @@ int main(int argc, char *argv[]) {
|
||||
char *buffer = readStringFromFile(argv[1]);
|
||||
|
||||
//printf("%s\n", buffer);
|
||||
Tokenizer tokenizer = {
|
||||
/*Tokenizer tokenizer = {
|
||||
.source = buffer,
|
||||
.column = 1,
|
||||
.line = 1,
|
||||
.pos = 0
|
||||
};
|
||||
};*/
|
||||
|
||||
assemble(&tokenizer);
|
||||
VMBL_Instruction program[VMBL_PROGRAM_SIZE];
|
||||
|
||||
assemble(buffer, program, VMBL_PROGRAM_SIZE);
|
||||
free(buffer);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -125,6 +125,8 @@ Token getCurrentToken(Tokenizer *tokenizer) {
|
||||
|
||||
default:
|
||||
if (isalpha(*tokenizer->source)) {
|
||||
token.columnStart = tokenizer->column;
|
||||
|
||||
char *tokenValue = parseName(tokenizer);
|
||||
token.value = tokenValue;
|
||||
|
||||
@@ -140,6 +142,7 @@ Token getCurrentToken(Tokenizer *tokenizer) {
|
||||
|
||||
}
|
||||
} else if (isdigit(*tokenizer->source)) {
|
||||
token.columnStart = token.columnStart;
|
||||
char *tokenValue = parseNumber(tokenizer);
|
||||
token.value = tokenValue;
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ typedef struct
|
||||
TokenType type;
|
||||
char* value;
|
||||
unsigned int line;
|
||||
unsigned int columnStart;
|
||||
} Token;
|
||||
|
||||
typedef struct
|
||||
|
||||
Reference in New Issue
Block a user