Compare commits
41 Commits
ec23d55f9d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| d011d2beb4 | |||
| e3702f46f3 | |||
| ca861f7e4b | |||
| 9796e309cf | |||
| a18479b21c | |||
| b502184478 | |||
| b348ca7626 | |||
| 1570177de1 | |||
| 2da11f854d | |||
| 337c6ada24 | |||
| bb0baba167 | |||
| ca0b87aacd | |||
| e8c49508a0 | |||
| 68182d916e | |||
| 3f684dad3f | |||
| 03aab86413 | |||
| c382ecd867 | |||
| 228d2ac762 | |||
| b289448f56 | |||
| a3ca979133 | |||
| 3e9ce0dfc0 | |||
| 063e85d24a | |||
| 07474d4317 | |||
| 51639e904d | |||
| c728801bc3 | |||
| e3c8a2f453 | |||
| bf68f1500c | |||
| 31577fcc62 | |||
| 925077d55e | |||
| 32d6a029dd | |||
| dac983b684 | |||
| d3c03b4987 | |||
| 0f155c80be | |||
| 4680597065 | |||
| c6762a7966 | |||
| 792aed13ae | |||
| 08b1edd7a7 | |||
| 8eef78c310 | |||
| 6d0dd99406 | |||
| 7717a40574 | |||
| bbf2277b6f |
29
README.md
29
README.md
@@ -75,15 +75,15 @@ build
|
||||
- [x] String operations
|
||||
- [x] Maths
|
||||
- [x] Comparisions
|
||||
- [ ] Type conversions
|
||||
- [x] Type conversions
|
||||
- [x] Functions
|
||||
- [x] Define functions
|
||||
- [x] Call functions
|
||||
- [x] Return values (type checked)
|
||||
- [x] Arguments for functions
|
||||
- [x] Jumping within functions
|
||||
- [ ] Custom data structures
|
||||
- [ ] Working with external libraries
|
||||
- [x] Custom data structures
|
||||
- [x] Working with external libraries
|
||||
|
||||
## Debugger
|
||||
|
||||
@@ -98,3 +98,26 @@ Commands:
|
||||
* eval (code): Runs Ground code in the current scope
|
||||
* help: Shows a help message
|
||||
|
||||
## Compiler
|
||||
|
||||
CGround now includes an experimental Ground -> x86_64 Linux ASM compiler. You can try it by adding the `-c` flag when running Ground. This will print the generated ASM to the console.
|
||||
|
||||
At present only the `int` data type is supported.
|
||||
|
||||
Supported instructions so far:
|
||||
|
||||
* set
|
||||
* add
|
||||
* subtract
|
||||
* multiply
|
||||
* equal
|
||||
* inequal
|
||||
* greater
|
||||
* lesser
|
||||
* not
|
||||
* jump
|
||||
* if
|
||||
* @ (label creation)
|
||||
* print
|
||||
* println
|
||||
* end
|
||||
|
||||
@@ -362,3 +362,23 @@ Looks in the path $GROUND_LIBS/`$libraryName`.grnd for the library. ($GROUND_LIB
|
||||
Attempts to import a shared library written in a compiled language like C or C++ for usage within the current program.
|
||||
|
||||
Looks in the path $GROUND_LIBS/`$libraryName`.so for the library. ($GROUND_LIBS is a system environment variable.)
|
||||
|
||||
### Data Structures
|
||||
|
||||
#### struct -structName
|
||||
|
||||
Creates a new struct which can be initialised. Until the endstruct keyword, the only valid instructions are init, set, fun, endfun, struct, and endstruct.
|
||||
|
||||
Any value created inside the struct will be added to the struct.
|
||||
|
||||
#### endstruct
|
||||
|
||||
Ends the creation of a struct.
|
||||
|
||||
#### getfield $object &fieldName &outputVar
|
||||
|
||||
Gets a field from an initialised object. fieldName must be a valid name of a field in the object. Errors if the field does not exist.
|
||||
|
||||
#### setfield &object &fieldName $value
|
||||
|
||||
Sets a field to a new value in the object. The value must be of the same type as the field's old value.
|
||||
|
||||
@@ -14,17 +14,6 @@ extern "C" {
|
||||
struct GroundScope;
|
||||
typedef struct GroundScope GroundScope;
|
||||
|
||||
/*
|
||||
* Stores data associated with an error thrown during Ground execution.
|
||||
*/
|
||||
typedef struct GroundError {
|
||||
char* what;
|
||||
char* type;
|
||||
struct GroundInstruction* where;
|
||||
size_t line;
|
||||
bool hasLine;
|
||||
} GroundError;
|
||||
|
||||
// Creates a GroundValue containing (in), with type ERROR.
|
||||
GroundValue createErrorGroundValue(GroundError in);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifndef LIBGROUND_H
|
||||
#define LIBGROUND_H
|
||||
#define MAX_ID_LEN 64
|
||||
|
||||
/*
|
||||
* groundvm.h
|
||||
@@ -11,13 +12,15 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <uthash.h>
|
||||
|
||||
|
||||
typedef enum GroundInstType {
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
||||
} GroundInstType;
|
||||
|
||||
typedef enum GroundValueType {
|
||||
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, NONE
|
||||
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE
|
||||
} GroundValueType;
|
||||
|
||||
typedef enum GroundArgType {
|
||||
@@ -30,6 +33,7 @@ typedef enum ListAccessStatus {
|
||||
|
||||
struct GroundValue;
|
||||
struct GroundFunction;
|
||||
struct GroundStruct;
|
||||
|
||||
struct List;
|
||||
|
||||
@@ -41,20 +45,34 @@ typedef struct List {
|
||||
struct GroundValue* values;
|
||||
} List;
|
||||
|
||||
/*
|
||||
* Stores data associated with an error thrown during Ground execution.
|
||||
*/
|
||||
typedef struct GroundError {
|
||||
char* what;
|
||||
char* type;
|
||||
struct GroundInstruction* where;
|
||||
size_t line;
|
||||
bool hasLine;
|
||||
} GroundError;
|
||||
|
||||
/*
|
||||
* Stores literal values created in a Ground program.
|
||||
*/
|
||||
typedef struct GroundValue {
|
||||
GroundValueType type;
|
||||
union {
|
||||
struct GroundStruct* customType;
|
||||
struct {
|
||||
int64_t intVal;
|
||||
double doubleVal;
|
||||
char* stringVal;
|
||||
char charVal;
|
||||
bool boolVal;
|
||||
List listVal;
|
||||
GroundError errorVal;
|
||||
struct GroundFunction* fnVal;
|
||||
void* customVal;
|
||||
struct GroundStruct* structVal;
|
||||
struct GroundObject* customVal;
|
||||
} data;
|
||||
} GroundValue;
|
||||
|
||||
@@ -115,6 +133,57 @@ typedef struct GroundFunction {
|
||||
size_t startLine;
|
||||
} GroundFunction;
|
||||
|
||||
/*
|
||||
* Field for a GroundStruct
|
||||
*/
|
||||
typedef struct GroundStructField {
|
||||
char id[64];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
} GroundStructField;
|
||||
|
||||
/*
|
||||
* Represents a Ground struct.
|
||||
*/
|
||||
typedef struct GroundStruct {
|
||||
GroundStructField* fields;
|
||||
size_t size;
|
||||
} GroundStruct;
|
||||
|
||||
/*
|
||||
* Field for a GroundObject
|
||||
*/
|
||||
typedef struct GroundObjectField {
|
||||
char id[64];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
} GroundObjectField;
|
||||
|
||||
/*
|
||||
* Represents an initialised Ground struct.
|
||||
*/
|
||||
typedef struct GroundObject {
|
||||
GroundObjectField* fields;
|
||||
} GroundObject;
|
||||
|
||||
typedef struct GroundLabel {
|
||||
char id[MAX_ID_LEN];
|
||||
int lineNum;
|
||||
UT_hash_handle hh;
|
||||
} GroundLabel;
|
||||
|
||||
typedef struct GroundVariable {
|
||||
char id[MAX_ID_LEN];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
} GroundVariable;
|
||||
|
||||
typedef struct GroundScope {
|
||||
GroundLabel** labels;
|
||||
GroundVariable** variables;
|
||||
bool isMainScope;
|
||||
} GroundScope;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -123,16 +192,22 @@ GroundProgram groundCreateProgram();
|
||||
void groundAddInstructionToProgram(GroundProgram* program, GroundInstruction instruction);
|
||||
GroundValue groundRunProgram(GroundProgram* program);
|
||||
void groundPrintProgram(GroundProgram* program);
|
||||
char* groundCompileProgram(GroundProgram* program);
|
||||
|
||||
GroundInstruction groundCreateInstruction(GroundInstType type);
|
||||
void groundAddValueToInstruction(GroundInstruction* inst, GroundValue value);
|
||||
void groundAddReferenceToInstruction(GroundInstruction* inst, GroundArg value);
|
||||
GroundArg groundCreateReference(GroundArgType type, char* ref);
|
||||
|
||||
void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value);
|
||||
|
||||
GroundValue groundCreateValue(GroundValueType type, ...);
|
||||
|
||||
GroundProgram groundParseFile(const char* code);
|
||||
|
||||
GroundStruct groundCreateStruct();
|
||||
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
152
libs/math/math.c
152
libs/math/math.c
@@ -1,5 +1,12 @@
|
||||
#include <groundext.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
|
||||
|
||||
GroundValue ground_sin(GroundScope* scope, List args) {
|
||||
return groundCreateValue(
|
||||
@@ -21,19 +28,15 @@ GroundValue ground_tan(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue rad_to_deg(GroundScope* scope, List args) {
|
||||
double radians = args.values[0].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
radians * (180.0 / M_PI)
|
||||
args.values[0].data.doubleVal * (180.0 / M_PI)
|
||||
);
|
||||
}
|
||||
GroundValue deg_to_rad(GroundScope* scope, List args) {
|
||||
double deg = args.values[0].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
deg * (M_PI / 180.0)
|
||||
args.values[0].data.doubleVal * (M_PI / 180.0)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -62,16 +65,149 @@ GroundValue ground_sqrt(GroundScope* scope, List args) {
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_abs(GroundScope* scope, List args) {
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
abs(args.values[0].data.doubleVal)
|
||||
);
|
||||
}
|
||||
GroundValue ground_min(GroundScope* scope, List args) {
|
||||
double a = args.values[0].data.doubleVal;
|
||||
double b = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
(a < b) ? a : b
|
||||
);
|
||||
}
|
||||
GroundValue ground_max(GroundScope* scope, List args) {
|
||||
double a = args.values[0].data.doubleVal;
|
||||
double b = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
(a > b) ? a : b
|
||||
);
|
||||
}
|
||||
GroundValue ground_clamp(GroundScope* scope, List args) {
|
||||
double number = args.values[0].data.doubleVal;
|
||||
double min = args.values[1].data.doubleVal;
|
||||
double max = args.values[2].data.doubleVal;
|
||||
|
||||
if (number < min) number = min;
|
||||
if (number > max) number = max;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
number
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_round(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= 0.0)
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x + 0.5)
|
||||
);
|
||||
else
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x - 0.5)
|
||||
);
|
||||
}
|
||||
GroundValue ground_floor(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x < 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i-1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
GroundValue ground_ceil(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x > 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i+1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_random(GroundScope* scope, List args) {
|
||||
int64_t min = args.values[0].data.intVal;
|
||||
int64_t max = args.values[1].data.intVal;
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
min + rand() % (max - min)
|
||||
);
|
||||
}
|
||||
GroundValue ground_random_double(GroundScope* scope, List args) {
|
||||
double min = args.values[0].data.doubleVal;
|
||||
double max = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
min + (double)rand() / RAND_MAX * (max - min)
|
||||
);
|
||||
}
|
||||
GroundValue ground_random_set_seed(GroundScope* scope, List args) {
|
||||
srand(args.values[0].data.intVal);
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
void ground_init(GroundScope* scope) {
|
||||
srand((unsigned)time(NULL) ^ (unsigned)clock());
|
||||
|
||||
groundAddNativeFunction(scope, "math_Sin", ground_sin, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Cos", ground_cos, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Tan", ground_tan, DOUBLE, 1, DOUBLE, "radians");
|
||||
|
||||
groundAddNativeFunction(scope, "math_DegreesToRadians", deg_to_rad, DOUBLE, 1, DOUBLE, "degrees");
|
||||
groundAddNativeFunction(scope, "math_RadiansToDegrees", rad_to_deg, DOUBLE, 1, DOUBLE, "radians");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Modulos", ground_modulos, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Pow", ground_pow, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Sqrt", ground_sqrt, DOUBLE, 1, DOUBLE, "number");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Abs", ground_abs, DOUBLE, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Min", ground_min, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Max", ground_max, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Clamp", ground_clamp, DOUBLE, 2, DOUBLE, "number", DOUBLE, "min", DOUBLE, "max");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Round", ground_round, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Floor", ground_floor, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Ceil", ground_ceil, INT, 1, DOUBLE, "number");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Random", ground_random, INT, 2, INT, "min", INT, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomDouble", ground_random_double, DOUBLE, 2, DOUBLE, "min", DOUBLE, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomSetSeed", ground_random_set_seed, INT, 1, INT, "seed");
|
||||
}
|
||||
541
src/compiler.c
Normal file
541
src/compiler.c
Normal file
@@ -0,0 +1,541 @@
|
||||
#include "compiler.h"
|
||||
#include "interpreter.h"
|
||||
#include "types.h"
|
||||
#include "include/estr.h"
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
VariableTable createVariableTable() {
|
||||
VariableTable vt;
|
||||
vt.capacity = 16;
|
||||
vt.count = 0;
|
||||
vt.vars = malloc(sizeof(VariableInfo) * vt.capacity);
|
||||
return vt;
|
||||
}
|
||||
|
||||
void addVtVariable(VariableTable* vt, const char* name) {
|
||||
// check if it already exists
|
||||
for (size_t i = 0; i < vt->count; i++) {
|
||||
if (strcmp(vt->vars[i].name, name) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// increase capacity
|
||||
if (vt->count >= vt->capacity) {
|
||||
vt->capacity *= 2;
|
||||
vt->vars = realloc(vt->vars, sizeof(VariableInfo) * vt->capacity);
|
||||
}
|
||||
|
||||
// add the variable
|
||||
strcpy(vt->vars[vt->count].name, name);
|
||||
vt->vars[vt->count].offset = vt->count * 8;
|
||||
vt->count++;
|
||||
}
|
||||
|
||||
int getVariablePos(VariableTable* vt, const char* name) {
|
||||
for (size_t i = 0; i < vt->count; i++) {
|
||||
if (strcmp(vt->vars[i].name, name) == 0) {
|
||||
return vt->vars[i].offset;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* processValueString(GroundArg arg) {
|
||||
if (arg.type == VALREF) {
|
||||
char* buf = malloc(sizeof(char) * 260);
|
||||
snprintf(buf, sizeof(char) * 260, "[%s]", arg.value.refName);
|
||||
return buf;
|
||||
}
|
||||
if (arg.type == VALUE) {
|
||||
if (arg.value.value.type != INT) {
|
||||
printf("Only int is supported right now\n");
|
||||
exit(1);
|
||||
}
|
||||
char* buf = malloc(sizeof(char) * 64);
|
||||
snprintf(buf, sizeof(char) * 260, "%" PRId64, arg.value.value.data.intVal);
|
||||
return buf;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* compileGroundProgram(GroundProgram* program) {
|
||||
Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n");
|
||||
Estr data = CREATE_ESTR("section .bss\n");
|
||||
Estr helpers = CREATE_ESTR("");
|
||||
|
||||
APPEND_ESTR(helpers, "\n; Helper: Print integer in rax\n");
|
||||
APPEND_ESTR(helpers, "print_int:\n");
|
||||
APPEND_ESTR(helpers, " push rbp\n");
|
||||
APPEND_ESTR(helpers, " mov rbp, rsp\n");
|
||||
APPEND_ESTR(helpers, " sub rsp, 32 ; Allocate buffer on stack\n");
|
||||
APPEND_ESTR(helpers, " mov rdi, rsp ; RDI = buffer pointer\n");
|
||||
APPEND_ESTR(helpers, " add rdi, 31 ; Point to end of buffer\n");
|
||||
APPEND_ESTR(helpers, " mov byte [rdi], 0 ; Null terminator\n");
|
||||
APPEND_ESTR(helpers, " dec rdi\n");
|
||||
APPEND_ESTR(helpers, " mov rbx, 10 ; Divisor\n");
|
||||
APPEND_ESTR(helpers, " test rax, rax\n");
|
||||
APPEND_ESTR(helpers, " jns .positive\n");
|
||||
APPEND_ESTR(helpers, " neg rax ; Make positive\n");
|
||||
APPEND_ESTR(helpers, " push rax\n");
|
||||
APPEND_ESTR(helpers, " mov byte [rdi], '-'\n");
|
||||
APPEND_ESTR(helpers, " dec rdi\n");
|
||||
APPEND_ESTR(helpers, " pop rax\n");
|
||||
APPEND_ESTR(helpers, ".positive:\n");
|
||||
APPEND_ESTR(helpers, " xor rcx, rcx ; Digit counter\n");
|
||||
APPEND_ESTR(helpers, ".convert_loop:\n");
|
||||
APPEND_ESTR(helpers, " xor rdx, rdx\n");
|
||||
APPEND_ESTR(helpers, " div rbx ; rax = rax/10, rdx = remainder\n");
|
||||
APPEND_ESTR(helpers, " add dl, '0' ; Convert to ASCII\n");
|
||||
APPEND_ESTR(helpers, " mov [rdi], dl\n");
|
||||
APPEND_ESTR(helpers, " dec rdi\n");
|
||||
APPEND_ESTR(helpers, " inc rcx\n");
|
||||
APPEND_ESTR(helpers, " test rax, rax\n");
|
||||
APPEND_ESTR(helpers, " jnz .convert_loop\n");
|
||||
APPEND_ESTR(helpers, " inc rdi ; Point back to first digit\n");
|
||||
APPEND_ESTR(helpers, " ; Now print the string\n");
|
||||
APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n");
|
||||
APPEND_ESTR(helpers, " mov rsi, rdi ; Buffer\n");
|
||||
APPEND_ESTR(helpers, " mov rdx, rcx ; Length\n");
|
||||
APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n");
|
||||
APPEND_ESTR(helpers, " syscall\n");
|
||||
APPEND_ESTR(helpers, " mov rsp, rbp\n");
|
||||
APPEND_ESTR(helpers, " pop rbp\n");
|
||||
APPEND_ESTR(helpers, " ret\n");
|
||||
|
||||
APPEND_ESTR(helpers, "\n; Helper: Print string at address in rax, length in rbx\n");
|
||||
APPEND_ESTR(helpers, "print_string:\n");
|
||||
APPEND_ESTR(helpers, " push rax\n");
|
||||
APPEND_ESTR(helpers, " push rbx\n");
|
||||
APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n");
|
||||
APPEND_ESTR(helpers, " mov rsi, rax ; String address\n");
|
||||
APPEND_ESTR(helpers, " mov rdx, rbx ; Length\n");
|
||||
APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n");
|
||||
APPEND_ESTR(helpers, " syscall\n");
|
||||
APPEND_ESTR(helpers, " pop rbx\n");
|
||||
APPEND_ESTR(helpers, " pop rax\n");
|
||||
APPEND_ESTR(helpers, " ret\n");
|
||||
|
||||
VariableTable varTable = createVariableTable();
|
||||
|
||||
// Allocate a spot for all direct references
|
||||
for (size_t i = 0; i < program->size; i++) {
|
||||
GroundInstruction gi = program->instructions[i];
|
||||
for (size_t j = 0; j < gi.args.length; j++) {
|
||||
if (gi.args.args[j].type == DIRREF) {
|
||||
addVtVariable(&varTable, gi.args.args[j].value.refName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create data section
|
||||
for (size_t i = 0; i < varTable.count; i++) {
|
||||
if (strcmp(varTable.vars[i].name, "") == 0) {
|
||||
continue;
|
||||
}
|
||||
APPEND_ESTR(data, " ");
|
||||
APPEND_ESTR(data, varTable.vars[i].name);
|
||||
APPEND_ESTR(data, " resq 1\n");
|
||||
}
|
||||
|
||||
// Generate assembly
|
||||
for (size_t i = 0; i < program->size; i++) {
|
||||
GroundInstruction gi = program->instructions[i];
|
||||
switch (gi.type) {
|
||||
case CREATELABEL: {
|
||||
APPEND_ESTR(start, gi.args.args[0].value.refName);
|
||||
APPEND_ESTR(start, ":\n");
|
||||
break;
|
||||
}
|
||||
case SET: {
|
||||
if (gi.args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 2", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[0].value.refName;
|
||||
|
||||
APPEND_ESTR(start, " ; set\n")
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
|
||||
break;
|
||||
}
|
||||
case JUMP: {
|
||||
if (gi.args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 1) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != LINEREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a LineRef", &gi, i);
|
||||
}
|
||||
|
||||
char* labelName = gi.args.args[0].value.refName;
|
||||
|
||||
APPEND_ESTR(start, " ; jump\n");
|
||||
APPEND_ESTR(start, " jmp ");
|
||||
APPEND_ESTR(start, labelName);
|
||||
APPEND_ESTR(start, "\n");
|
||||
|
||||
break;
|
||||
}
|
||||
case IF: {
|
||||
if (gi.args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int (for now) for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != LINEREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a LineRef", &gi, i);
|
||||
}
|
||||
|
||||
char* labelName = gi.args.args[1].value.refName;
|
||||
|
||||
APPEND_ESTR(start, " ; if\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " test rax, rax\n");
|
||||
APPEND_ESTR(start, " jnz ");
|
||||
APPEND_ESTR(start, labelName);
|
||||
APPEND_ESTR(start, "\n");
|
||||
|
||||
break;
|
||||
}
|
||||
case NOT: {
|
||||
if (gi.args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int (for now) for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef", &gi, i);
|
||||
}
|
||||
|
||||
char* varName = gi.args.args[1].value.refName;
|
||||
|
||||
APPEND_ESTR(start, " ; not\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " xor rax, 1\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
|
||||
break;
|
||||
}
|
||||
case ADD: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for add instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for add instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; add\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " add rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case SUBTRACT: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for subtract instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for subtract instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; subtract\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " sub rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case MULTIPLY: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for multiply instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for multiply instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; multiply\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " imul rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case EQUAL: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for equal instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for equal instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; equal\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " cmp rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " sete al\n");
|
||||
APPEND_ESTR(start, " movzx rax, al\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case INEQUAL: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for inequal instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for inequal instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; inequal\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " cmp rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " setne al\n");
|
||||
APPEND_ESTR(start, " movzx rax, al\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case GREATER: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for greater instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for greater instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; greater\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " cmp rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " setg al\n");
|
||||
APPEND_ESTR(start, " movzx rax, al\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case LESSER: {
|
||||
if (gi.args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for lesser instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for lesser instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
|
||||
}
|
||||
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
|
||||
}
|
||||
if (gi.args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
|
||||
}
|
||||
|
||||
char* varName= gi.args.args[2].value.refName;
|
||||
APPEND_ESTR(start, " ; lesser\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " cmp rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[1]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " setl al\n");
|
||||
APPEND_ESTR(start, " movzx rax, al\n");
|
||||
APPEND_ESTR(start, " mov [");
|
||||
APPEND_ESTR(start, varName);
|
||||
APPEND_ESTR(start, "], rax\n");
|
||||
break;
|
||||
}
|
||||
case PRINT:
|
||||
case PRINTLN: {
|
||||
if (gi.args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", &gi, i);
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < gi.args.length; j++) {
|
||||
if (gi.args.args[j].type != VALUE && gi.args.args[j].type != VALREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", &gi, i);
|
||||
}
|
||||
if (j > 0) {
|
||||
// Print space between arguments
|
||||
APPEND_ESTR(start, " ; print space\n");
|
||||
APPEND_ESTR(start, " mov rax, 1\n");
|
||||
APPEND_ESTR(start, " mov rdi, 1\n");
|
||||
APPEND_ESTR(start, " lea rsi, [rel space_char]\n");
|
||||
APPEND_ESTR(start, " mov rdx, 1\n");
|
||||
APPEND_ESTR(start, " syscall\n");
|
||||
}
|
||||
APPEND_ESTR(start, " ; print int\n");
|
||||
APPEND_ESTR(start, " mov rax, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[j]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " call print_int\n");
|
||||
}
|
||||
|
||||
if (gi.type == PRINTLN) {
|
||||
APPEND_ESTR(start, " ; print newline\n");
|
||||
APPEND_ESTR(start, " mov rax, 1\n");
|
||||
APPEND_ESTR(start, " mov rdi, 1\n");
|
||||
APPEND_ESTR(start, " lea rsi, [rel newline_char]\n");
|
||||
APPEND_ESTR(start, " mov rdx, 1\n");
|
||||
APPEND_ESTR(start, " syscall\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case END: {
|
||||
if (gi.args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 1) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
||||
}
|
||||
APPEND_ESTR(start, " ; end\n");
|
||||
APPEND_ESTR(start, " mov rax, 60\n");
|
||||
APPEND_ESTR(start, " mov rdi, ");
|
||||
APPEND_ESTR(start, processValueString(gi.args.args[0]));
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " syscall\n");
|
||||
break;
|
||||
}
|
||||
case DROP: {
|
||||
// Drop does nothing in ground->asm as we use the stack
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
printf("no\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Estr complete = CREATE_ESTR("");
|
||||
APPEND_ESTR(complete, start.str);
|
||||
APPEND_ESTR(complete, " ; End of program\n mov rax, 60\n mov rdi, 0\n syscall\n");
|
||||
APPEND_ESTR(complete, helpers.str);
|
||||
APPEND_ESTR(complete, "\nsection .rodata\n");
|
||||
APPEND_ESTR(complete, "newline_char: db 10\n");
|
||||
APPEND_ESTR(complete, "space_char: db 32\n");
|
||||
APPEND_ESTR(complete, data.str)
|
||||
|
||||
return complete.str;
|
||||
}
|
||||
18
src/compiler.h
Normal file
18
src/compiler.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "types.h"
|
||||
#include "interpreter.h"
|
||||
|
||||
char* compileGroundProgram(GroundProgram* program);
|
||||
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine);
|
||||
|
||||
typedef struct VariableInfo {
|
||||
char name[256];
|
||||
int offset;
|
||||
} VariableInfo;
|
||||
|
||||
typedef struct VariableTable {
|
||||
VariableInfo* vars;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
} VariableTable;
|
||||
|
||||
|
||||
52
src/include/estr.h
Normal file
52
src/include/estr.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef ESTR_H
|
||||
#define ESTR_H
|
||||
|
||||
/*
|
||||
|
||||
estr.h - Easy string manipulation
|
||||
This library has macros to allow easier manipulation of strings. No longer shall
|
||||
you have to malloc and realloc away to keep adding to your strings.
|
||||
|
||||
Usage:
|
||||
|
||||
Estr myString = CREATE_ESTR("my awesome string");
|
||||
APPEND_ESTR(myString, " is so cool");
|
||||
printf("%s\n", myString.str);
|
||||
|
||||
*/
|
||||
|
||||
#define CREATE_ESTR(instr) \
|
||||
(Estr) { \
|
||||
.str = instr,\
|
||||
.size = strlen(instr),\
|
||||
.shouldBeFreed = 0, \
|
||||
.destroyed = 0 \
|
||||
}
|
||||
|
||||
#define APPEND_ESTR(estr, instr) { \
|
||||
estr.size = estr.size + strlen(instr); \
|
||||
char* tmp_ptr = malloc(estr.size + 1); \
|
||||
if (tmp_ptr == NULL) printf("WARNING: Could not realloc estr " #estr "\n"); \
|
||||
else { \
|
||||
snprintf(tmp_ptr, estr.size + 1, "%s%s", estr.str, instr); \
|
||||
if (estr.shouldBeFreed > 0) free(estr.str); \
|
||||
estr.shouldBeFreed = 1; \
|
||||
estr.str = tmp_ptr; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define DESTROY_ESTR(estr) if (estr.shouldBeFreed > 0 && estr.destroyed < 1) free(estr.str);
|
||||
|
||||
typedef struct Estr {
|
||||
char* str;
|
||||
size_t size;
|
||||
int8_t shouldBeFreed;
|
||||
int8_t destroyed;
|
||||
} Estr;
|
||||
|
||||
#endif // ESTR_H
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "interpreter.h"
|
||||
#include "compiler.h"
|
||||
#include "types.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
@@ -68,11 +68,30 @@ GroundValue groundCreateValue(GroundValueType type, ...) {
|
||||
return createListGroundValue(va_arg(args, List));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
case FUNCTION: {
|
||||
return createFunctionGroundValue(va_arg(args, GroundFunction*));
|
||||
break;
|
||||
}
|
||||
case STRUCTVAL: {
|
||||
GroundValue gv;
|
||||
gv.type = STRUCTVAL;
|
||||
gv.data.structVal = malloc(sizeof(GroundStruct));
|
||||
*gv.data.structVal = va_arg(args, GroundStruct);
|
||||
return gv;
|
||||
}
|
||||
case NONE: {
|
||||
return createNoneGroundValue();
|
||||
break;
|
||||
}
|
||||
case ERROR:
|
||||
case CUSTOM: {
|
||||
// FIXME
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return createNoneGroundValue();
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -90,3 +109,22 @@ void groundPrintProgram(GroundProgram* program) {
|
||||
GroundProgram groundParseFile(const char* code) {
|
||||
return parseFile(code);
|
||||
}
|
||||
|
||||
GroundStruct groundCreateStruct() {
|
||||
GroundStruct gs;
|
||||
gs.size = 0;
|
||||
gs.fields = malloc(sizeof(GroundStructField));
|
||||
return gs;
|
||||
}
|
||||
|
||||
void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value) {
|
||||
addVariable(gs->variables, name, value);
|
||||
}
|
||||
|
||||
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field) {
|
||||
addFieldToStruct(gstruct, name, field);
|
||||
}
|
||||
|
||||
char* groundCompileProgram(GroundProgram* program) {
|
||||
return compileGroundProgram(program);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef _WIN32
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -13,6 +15,23 @@ int currentInstruction = 0;
|
||||
|
||||
bool isMainScopeGlobal = true;
|
||||
|
||||
[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
printGroundArg(&type);
|
||||
printf("\n ErrorContext: ");
|
||||
printGroundArg(&what);
|
||||
printf("\n");
|
||||
if (where != NULL) {
|
||||
printf(" ErrorInstruction: ");
|
||||
printGroundInstruction(where);
|
||||
printf("\n");
|
||||
}
|
||||
if (whereLine > -1) {
|
||||
printf(" ErrorLine: %d\n", whereLine + 1);
|
||||
}
|
||||
exit(exitCode);
|
||||
}
|
||||
|
||||
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
switch (error) {
|
||||
@@ -335,7 +354,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
|
||||
|
||||
GroundValue gv;
|
||||
|
||||
switch (stringToValueType(in->instructions[i].args.args[0].value.refName)) {
|
||||
switch (stringToValueType(in->instructions[i].args.args[1].value.refName)) {
|
||||
case INT: {
|
||||
gv = createIntGroundValue(0);
|
||||
break;
|
||||
@@ -426,6 +445,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
|
||||
addInstructionToProgram(&gp, in->instructions[i]);
|
||||
i++;
|
||||
}
|
||||
i--;
|
||||
|
||||
GroundFunction* function = parseFunction(&gp, errorOffset);
|
||||
function->startLine = i;
|
||||
@@ -492,6 +512,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
|
||||
addInstructionToProgram(&gp, in->instructions[i]);
|
||||
i++;
|
||||
}
|
||||
i--;
|
||||
|
||||
GroundFunction* function = parseFunction(&gp, errorOffset);
|
||||
function->startLine = i;
|
||||
@@ -529,6 +550,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
|
||||
addInstructionToProgram(&gp, in->instructions[i]);
|
||||
i++;
|
||||
}
|
||||
i--;
|
||||
|
||||
GroundValue gv = {
|
||||
.type = STRUCTVAL,
|
||||
@@ -814,7 +836,19 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ERRORCMD: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
} else if (in->args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[2].value.value.type != INT ) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an int for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
customError(in->args.args[0], in->args.args[1], in, currentInstruction, in->args.args[2].value.value.data.intVal);
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* VARIABLES AND LISTS
|
||||
* These instructions are for initializing variables and lists.
|
||||
@@ -1075,7 +1109,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[2].value.refName, createIntGroundValue(atoll(in->args.args[0].value.value.data.stringVal)));
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue(atoll(in->args.args[0].value.value.data.stringVal)));
|
||||
break;
|
||||
}
|
||||
case STOD: {
|
||||
@@ -1091,7 +1125,39 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue(atof(in->args.args[0].value.value.data.stringVal)));
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createDoubleGroundValue(atof(in->args.args[0].value.value.data.stringVal)));
|
||||
break;
|
||||
}
|
||||
case ITOC: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE || in->args.args[0].value.value.type != INT) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createCharGroundValue((char)in->args.args[0].value.value.data.intVal));
|
||||
break;
|
||||
}
|
||||
case CTOI: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE || in->args.args[0].value.value.type != CHAR) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Char for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue((int)in->args.args[0].value.value.data.charVal));
|
||||
break;
|
||||
}
|
||||
case TOSTRING: {
|
||||
@@ -1108,57 +1174,53 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
GroundValue* value = &in->args.args[0].value.value;
|
||||
char buf[256];
|
||||
switch (value->type) {
|
||||
case INT: {
|
||||
char* buf = malloc(sizeof(char) * 256);
|
||||
snprintf(buf, sizeof(char) * 256, "%" PRId64, value->data.intVal);
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
|
||||
snprintf(buf, sizeof(buf) * 256, "%" PRId64, value->data.intVal);
|
||||
break;
|
||||
}
|
||||
case DOUBLE: {
|
||||
char* buf = malloc(sizeof(char) * 256);
|
||||
snprintf(buf, sizeof(char) * 256, "%f", value->data.doubleVal);
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
|
||||
snprintf(buf, sizeof(buf) * 256, "%f", value->data.doubleVal);
|
||||
break;
|
||||
}
|
||||
case STRING: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(value->data.stringVal));
|
||||
snprintf(buf, sizeof(buf), "%s", value->data.stringVal);
|
||||
break;
|
||||
}
|
||||
case CHAR: {
|
||||
char* buf = malloc(sizeof(char) * 2);
|
||||
buf[0] = value->data.charVal;
|
||||
buf[1] = '\0';
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
|
||||
break;
|
||||
}
|
||||
case BOOL: {
|
||||
if (value->data.boolVal) {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("true"));
|
||||
snprintf(buf, sizeof(buf), "true");
|
||||
} else {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("false"));
|
||||
snprintf(buf, sizeof(buf), "false");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LIST: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<list>"));
|
||||
snprintf(buf, sizeof(buf), "<list>");
|
||||
break;
|
||||
}
|
||||
case FUNCTION: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<function>"));
|
||||
snprintf(buf, sizeof(buf), "<function>");
|
||||
break;
|
||||
}
|
||||
case CUSTOM: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<custom>"));
|
||||
snprintf(buf, sizeof(buf), "<custom>");
|
||||
break;
|
||||
}
|
||||
case NONE:
|
||||
default: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<default>"));
|
||||
snprintf(buf, sizeof(buf), "<default>");
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -1865,6 +1927,10 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
}
|
||||
|
||||
case EXTERN: {
|
||||
#ifdef _WIN32
|
||||
runtimeError(FIXME, "No Windows support for extern, sucker", in, currentInstruction);
|
||||
#endif
|
||||
#ifndef _WIN32
|
||||
if (in->args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);
|
||||
}
|
||||
@@ -1914,6 +1980,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
}
|
||||
|
||||
initFn(scope);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1999,6 +2066,77 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* OBJECTS
|
||||
* Allows manipulation of Ground objects.
|
||||
*/
|
||||
// getfield $obj &field &result
|
||||
case GETFIELD: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Object for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].value.value.type != CUSTOM) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Object for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", in, currentInstruction);
|
||||
}
|
||||
|
||||
GroundObjectField* field = findField(*in->args.args[0].value.value.data.customVal, in->args.args[1].value.refName);
|
||||
if (field == NULL) {
|
||||
runtimeError(UNKNOWN_VARIABLE, "Struct does not contain that field", in, currentInstruction);
|
||||
}
|
||||
addVariable(scope->variables, in->args.args[2].value.refName, field->value);
|
||||
|
||||
break;
|
||||
}
|
||||
// setfield &obj &field $value
|
||||
case SETFIELD: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef to an Object for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[2].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 3", in, currentInstruction);
|
||||
}
|
||||
|
||||
GroundVariable* var = findVariable(*scope->variables, in->args.args[0].value.refName);
|
||||
if (var == NULL) {
|
||||
runtimeError(UNKNOWN_VARIABLE, "Could not find that object", in, currentInstruction);
|
||||
}
|
||||
if (var->value.type != CUSTOM) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef to an Object for arg 1", in, currentInstruction);
|
||||
}
|
||||
|
||||
GroundObjectField* field = findField(*var->value.data.customVal, in->args.args[1].value.refName);
|
||||
if (field == NULL) {
|
||||
runtimeError(UNKNOWN_VARIABLE, "Struct does not contain that field", in, currentInstruction);
|
||||
}
|
||||
if (field->value.type != in->args.args[2].value.value.type) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Field type and provided type do not match", in, currentInstruction);
|
||||
}
|
||||
|
||||
field->value = copyGroundValue(&in->args.args[2].value.value);
|
||||
break;
|
||||
}
|
||||
|
||||
case DROP: {
|
||||
if (in->args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);
|
||||
|
||||
@@ -42,6 +42,7 @@ GroundFunction* parseFunction(GroundProgram* in, size_t errorOffset);
|
||||
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope);
|
||||
GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);
|
||||
|
||||
void addVariable(GroundVariable **head, const char *id, GroundValue data);
|
||||
|
||||
|
||||
|
||||
|
||||
52
src/main.c
52
src/main.c
@@ -1,5 +1,7 @@
|
||||
#include "parser.h"
|
||||
#include "interpreter.h"
|
||||
#include "compiler.h"
|
||||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
char* getFileContents(const char* filename) {
|
||||
@@ -40,11 +42,55 @@ char* getFileContents(const char* filename) {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: ground [file]\n");
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
char* file = getFileContents(argv[1]);
|
||||
|
||||
bool compile = false;
|
||||
char* fileName = NULL;
|
||||
List groundArgs = createList();
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp("--compile", argv[i]) == 0 || strcmp("-c", argv[i]) == 0) {
|
||||
compile = true;
|
||||
}
|
||||
else if (strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
|
||||
printf("GroundVM help\n");
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
printf("Options:\n");
|
||||
printf(" -c or --compile: Outputs Linux x86_64 assembly instead of interpreting (WIP)\n");
|
||||
printf(" -h or --help: Shows this help message\n");
|
||||
exit(0);
|
||||
} else {
|
||||
if (fileName == NULL) {
|
||||
fileName = argv[i];
|
||||
} else {
|
||||
appendToList(&groundArgs, createStringGroundValue(argv[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName == NULL) {
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
printf("Error: No file name provided\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char* file = getFileContents(fileName);
|
||||
GroundProgram program = parseFile(file);
|
||||
free(file);
|
||||
interpretGroundProgram(&program, NULL);
|
||||
|
||||
if (compile) {
|
||||
char* compiled = compileGroundProgram(&program);
|
||||
printf("%s\n", compiled);
|
||||
} else {
|
||||
GroundVariable* variables = NULL;
|
||||
GroundLabel* labels = NULL;
|
||||
|
||||
GroundScope scope;
|
||||
scope.variables = &variables;
|
||||
scope.labels = &labels;
|
||||
scope.isMainScope = true;
|
||||
addVariable(scope.variables, "CMDLINE_ARGS", createListGroundValue(groundArgs));
|
||||
interpretGroundProgram(&program, &scope);
|
||||
}
|
||||
}
|
||||
|
||||
24
src/parser.c
24
src/parser.c
@@ -5,6 +5,25 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
size_t strnlen(const char *src, size_t n) {
|
||||
size_t len = 0;
|
||||
while (len < n && src[len])
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
char* strndup(const char *s, size_t n) {
|
||||
size_t len = strnlen(s, n);
|
||||
char *p = malloc(len + 1);
|
||||
if (p) {
|
||||
memcpy(p, s, len);
|
||||
p[len] = '\0';
|
||||
}
|
||||
return p;
|
||||
}
|
||||
#endif
|
||||
|
||||
GroundProgram createGroundProgram() {
|
||||
GroundProgram gp;
|
||||
gp.size = 0;
|
||||
@@ -137,6 +156,7 @@ static GroundInstType getInstructionType(const char* inst) {
|
||||
if (strcmp(inst, "input") == 0 || strcmp(inst, "stdin") == 0) return INPUT;
|
||||
if (strcmp(inst, "print") == 0 || strcmp(inst, "stdout") == 0) return PRINT;
|
||||
if (strcmp(inst, "println") == 0 || strcmp(inst, "stdlnout") == 0) return PRINTLN;
|
||||
if (strcmp(inst, "error") == 0) return ERRORCMD;
|
||||
if (strcmp(inst, "set") == 0) return SET;
|
||||
if (strcmp(inst, "gettype") == 0) return GETTYPE;
|
||||
if (strcmp(inst, "exists") == 0) return EXISTS;
|
||||
@@ -158,6 +178,8 @@ static GroundInstType getInstructionType(const char* inst) {
|
||||
if (strcmp(inst, "lesser") == 0) return LESSER;
|
||||
if (strcmp(inst, "stoi") == 0) return STOI;
|
||||
if (strcmp(inst, "stod") == 0) return STOD;
|
||||
if (strcmp(inst, "ctoi") == 0) return CTOI;
|
||||
if (strcmp(inst, "itoc") == 0) return ITOC;
|
||||
if (strcmp(inst, "tostring") == 0) return TOSTRING;
|
||||
if (strcmp(inst, "fun") == 0) return FUN;
|
||||
if (strcmp(inst, "return") == 0) return RETURN;
|
||||
@@ -167,6 +189,8 @@ static GroundInstType getInstructionType(const char* inst) {
|
||||
if (strcmp(inst, "struct") == 0) return STRUCT;
|
||||
if (strcmp(inst, "endstruct") == 0) return ENDSTRUCT;
|
||||
if (strcmp(inst, "init") == 0) return INIT;
|
||||
if (strcmp(inst, "getfield") == 0) return GETFIELD;
|
||||
if (strcmp(inst, "setfield") == 0) return SETFIELD;
|
||||
if (strcmp(inst, "use") == 0) return USE;
|
||||
if (strcmp(inst, "extern") == 0) return EXTERN;
|
||||
if (strcmp(inst, "drop") == 0) return DROP;
|
||||
|
||||
105
src/types.c
105
src/types.c
@@ -90,16 +90,55 @@ GroundValue copyGroundValue(const GroundValue* gv) {
|
||||
}
|
||||
case FUNCTION: newGv.data.fnVal = gv->data.fnVal; break;
|
||||
case STRUCTVAL: {
|
||||
// do this later lmao
|
||||
// FIXME
|
||||
newGv.data.structVal = gv->data.structVal;
|
||||
newGv.data.structVal = malloc(sizeof(GroundStruct));
|
||||
if (newGv.data.structVal == NULL) {
|
||||
printf("Couldn't allocate memory for GroundStruct copy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
newGv.data.structVal->size = gv->data.structVal->size;
|
||||
newGv.data.structVal->fields = malloc(gv->data.structVal->size * sizeof(GroundStructField));
|
||||
if (newGv.data.structVal->fields == NULL && gv->data.structVal->size > 0) {
|
||||
printf("Couldn't allocate memory for GroundStruct fields copy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < gv->data.structVal->size; i++) {
|
||||
strncpy(newGv.data.structVal->fields[i].id,
|
||||
gv->data.structVal->fields[i].id,
|
||||
sizeof(newGv.data.structVal->fields[i].id) - 1);
|
||||
newGv.data.structVal->fields[i].id[sizeof(newGv.data.structVal->fields[i].id) - 1] = '\0';
|
||||
|
||||
newGv.data.structVal->fields[i].value = copyGroundValue(&gv->data.structVal->fields[i].value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CUSTOM:
|
||||
// also do this later
|
||||
// FIXME
|
||||
newGv.data.customVal = gv->data.customVal;
|
||||
case CUSTOM: {
|
||||
newGv.customType = gv->customType;
|
||||
newGv.data.customVal = malloc(sizeof(GroundObject));
|
||||
if (newGv.data.customVal == NULL) {
|
||||
printf("Couldn't allocate memory for GroundObject copy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
newGv.data.customVal->fields = NULL;
|
||||
|
||||
GroundObjectField *field, *tmp;
|
||||
HASH_ITER(hh, gv->data.customVal->fields, field, tmp) {
|
||||
GroundObjectField* newField = malloc(sizeof(GroundObjectField));
|
||||
if (newField == NULL) {
|
||||
printf("Couldn't allocate memory for GroundObjectField copy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
strncpy(newField->id, field->id, sizeof(newField->id) - 1);
|
||||
newField->id[sizeof(newField->id) - 1] = '\0';
|
||||
newField->value = copyGroundValue(&field->value);
|
||||
|
||||
HASH_ADD_STR(newGv.data.customVal->fields, id, newField);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NONE:
|
||||
default: {
|
||||
|
||||
@@ -149,8 +188,47 @@ void printGroundValue(GroundValue* gv) {
|
||||
printf("<function>");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
printf("FIXME");
|
||||
case STRUCTVAL: {
|
||||
printf("<struct fields: { ");
|
||||
for (size_t i = 0; i < gv->data.structVal->size; i++) {
|
||||
if (i != 0) {
|
||||
printf(", ");
|
||||
}
|
||||
printf("%s: ", gv->data.structVal->fields[i].id);
|
||||
if (gv->data.structVal->fields[i].value.type == STRING) {
|
||||
printf("\"");
|
||||
printGroundValue(&gv->data.structVal->fields[i].value);
|
||||
printf("\"");
|
||||
} else {
|
||||
printGroundValue(&gv->data.structVal->fields[i].value);
|
||||
}
|
||||
}
|
||||
printf(" }>");
|
||||
break;
|
||||
}
|
||||
case CUSTOM: {
|
||||
printf("<object fields: { ");
|
||||
for (size_t i = 0; i < gv->customType->size; i++) {
|
||||
if (i != 0) {
|
||||
printf(", ");
|
||||
}
|
||||
printf("%s: ", gv->customType->fields[i].id);
|
||||
GroundObjectField* field = findField(*gv->data.customVal, gv->customType->fields[i].id);
|
||||
if (field == NULL) {
|
||||
printf("<missing>");
|
||||
} else {
|
||||
printGroundValue(&field->value);
|
||||
}
|
||||
}
|
||||
printf(" }>");
|
||||
break;
|
||||
}
|
||||
case ERROR: {
|
||||
printf("<error type: %s, what: %s>", gv->data.errorVal.type, gv->data.errorVal.what);
|
||||
break;
|
||||
}
|
||||
case NONE: {
|
||||
printf("<none>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -374,6 +452,12 @@ void printGroundInstruction(GroundInstruction* gi) {
|
||||
case STOD:
|
||||
printf("stod");
|
||||
break;
|
||||
case CTOI:
|
||||
printf("ctoi");
|
||||
break;
|
||||
case ITOC:
|
||||
printf("itoc");
|
||||
break;
|
||||
case TOSTRING:
|
||||
printf("tostring");
|
||||
break;
|
||||
@@ -412,6 +496,9 @@ void printGroundInstruction(GroundInstruction* gi) {
|
||||
break;
|
||||
case CREATELABEL:
|
||||
break;
|
||||
case ERRORCMD:
|
||||
printf("error");
|
||||
break;
|
||||
default:
|
||||
printf("FIXME");
|
||||
break;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "include/uthash.h"
|
||||
|
||||
typedef enum GroundInstType {
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
||||
} GroundInstType;
|
||||
|
||||
typedef enum GroundValueType {
|
||||
@@ -63,7 +63,7 @@ typedef struct GroundError {
|
||||
typedef struct GroundValue {
|
||||
GroundValueType type;
|
||||
struct GroundStruct* customType;
|
||||
union {
|
||||
struct {
|
||||
int64_t intVal;
|
||||
double doubleVal;
|
||||
char* stringVal;
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
tostring 32 &int
|
||||
tostring 32 &str
|
||||
stoi "12" &int
|
||||
stod "3.14" &dou
|
||||
itoc 353 &chr
|
||||
ctoi 'a' &it2
|
||||
|
||||
println $str
|
||||
println $int
|
||||
println $dou
|
||||
println $chr
|
||||
println $it2
|
||||
|
||||
2
tests/error.grnd
Normal file
2
tests/error.grnd
Normal file
@@ -0,0 +1,2 @@
|
||||
setlist &dat 1 2 3 "Hi!"
|
||||
error "Hello" $dat 1
|
||||
17
tests/struct.grnd
Normal file
17
tests/struct.grnd
Normal file
@@ -0,0 +1,17 @@
|
||||
struct -point
|
||||
|
||||
init &x -int
|
||||
init &y -int
|
||||
|
||||
endstruct
|
||||
|
||||
init &mypoint -point
|
||||
|
||||
setfield &mypoint &x 53
|
||||
setfield &mypoint &y 32
|
||||
|
||||
getfield $mypoint &x &value
|
||||
println $value
|
||||
|
||||
getfield $mypoint &y &value
|
||||
println $value
|
||||
Reference in New Issue
Block a user