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.
|
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 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
|
## Syntax
|
||||||
### Example "Hello, World!" Program
|
### Example "Hello, World!" Program
|
||||||
|
|||||||
9
fib.sasm
9
fib.sasm
@@ -1,6 +1,5 @@
|
|||||||
push 0
|
push 10
|
||||||
push 1
|
push 9
|
||||||
dup 1
|
add
|
||||||
dup 1
|
halt
|
||||||
add
|
add
|
||||||
jump 2
|
|
||||||
@@ -1,17 +1,22 @@
|
|||||||
#include "assembler.h"
|
#include "assembler.h"
|
||||||
|
#include "instructions.h"
|
||||||
#include <stdlib.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
|
#ifndef ASSEMBLER_H
|
||||||
#define 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
|
#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
|
#ifndef INSTRUCTIONS_H
|
||||||
#define INSTRUCTIONS_H
|
#define INSTRUCTIONS_H
|
||||||
|
|
||||||
|
#include "../vmbl.h"
|
||||||
|
|
||||||
#define MAX_ARGS 3
|
#define MAX_ARGS 3
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
ARG_TYPE_NONE,
|
||||||
ARG_TYPE_INT
|
ARG_TYPE_INT
|
||||||
} ArgType;
|
} ArgType;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char *mnemonic,
|
char *mnemonic;
|
||||||
uint8_t argCount,
|
uint8_t argCount;
|
||||||
ArgType args[MAX_ARGS]
|
ArgType args[MAX_ARGS];
|
||||||
} InstructionInfo;
|
} 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
|
#endif // !INSTRUCTIONS_H
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "tokenize.h"
|
|
||||||
#include "../file_utils.h"
|
#include "../file_utils.h"
|
||||||
#include "assembler.h"
|
#include "assembler.h"
|
||||||
|
#include "../vmbl.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
@@ -13,14 +13,16 @@ int main(int argc, char *argv[]) {
|
|||||||
char *buffer = readStringFromFile(argv[1]);
|
char *buffer = readStringFromFile(argv[1]);
|
||||||
|
|
||||||
//printf("%s\n", buffer);
|
//printf("%s\n", buffer);
|
||||||
Tokenizer tokenizer = {
|
/*Tokenizer tokenizer = {
|
||||||
.source = buffer,
|
.source = buffer,
|
||||||
.column = 1,
|
.column = 1,
|
||||||
.line = 1,
|
.line = 1,
|
||||||
.pos = 0
|
.pos = 0
|
||||||
};
|
};*/
|
||||||
|
|
||||||
assemble(&tokenizer);
|
VMBL_Instruction program[VMBL_PROGRAM_SIZE];
|
||||||
|
|
||||||
|
assemble(buffer, program, VMBL_PROGRAM_SIZE);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -125,6 +125,8 @@ Token getCurrentToken(Tokenizer *tokenizer) {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
if (isalpha(*tokenizer->source)) {
|
if (isalpha(*tokenizer->source)) {
|
||||||
|
token.columnStart = tokenizer->column;
|
||||||
|
|
||||||
char *tokenValue = parseName(tokenizer);
|
char *tokenValue = parseName(tokenizer);
|
||||||
token.value = tokenValue;
|
token.value = tokenValue;
|
||||||
|
|
||||||
@@ -140,6 +142,7 @@ Token getCurrentToken(Tokenizer *tokenizer) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
} else if (isdigit(*tokenizer->source)) {
|
} else if (isdigit(*tokenizer->source)) {
|
||||||
|
token.columnStart = token.columnStart;
|
||||||
char *tokenValue = parseNumber(tokenizer);
|
char *tokenValue = parseNumber(tokenizer);
|
||||||
token.value = tokenValue;
|
token.value = tokenValue;
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ typedef struct
|
|||||||
TokenType type;
|
TokenType type;
|
||||||
char* value;
|
char* value;
|
||||||
unsigned int line;
|
unsigned int line;
|
||||||
|
unsigned int columnStart;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|||||||
Reference in New Issue
Block a user