add function object
This commit is contained in:
@@ -1 +1 @@
|
|||||||
let x(hello,lol,world, WORLD,HELLOOOO,LOLLLLLL, WORLD)="HELLOOOO WORLD"
|
let x(hello,lol,world, WORLD,HELLOOOO,LOLLLLLL, WORLD)=let f(x)="bruh"
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#ifndef FUNCTION_H
|
|
||||||
#define FUNCTION_H
|
|
||||||
#include "../object.h"
|
|
||||||
|
|
||||||
|
|
||||||
#endif // FUNCTION_H
|
|
||||||
46
src/runtime/objects/functions/functions.c
Normal file
46
src/runtime/objects/functions/functions.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include "../../runtime.h"
|
||||||
|
#include "../object.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
ArgonObject *ARGON_FUNCTION_TYPE = NULL;
|
||||||
|
|
||||||
|
void init_function_type() {
|
||||||
|
ARGON_FUNCTION_TYPE = init_argon_class("Function");
|
||||||
|
}
|
||||||
|
|
||||||
|
void load_argon_function(Translated *translated, RuntimeState *state,
|
||||||
|
struct Stack stack) {
|
||||||
|
ArgonObject *object = init_child_argon_object(ARGON_FUNCTION_TYPE);
|
||||||
|
object->type = TYPE_FUNCTION;
|
||||||
|
uint64_t offset = pop_bytecode(translated, state);
|
||||||
|
uint64_t length = pop_bytecode(translated, state);
|
||||||
|
object->name = ar_alloc_atomic(length + 1);
|
||||||
|
memcpy(object->name, arena_get(&translated->constants, offset), length);
|
||||||
|
object->name[length] = '\0';
|
||||||
|
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
|
||||||
|
object->value.argon_fn.parameters =
|
||||||
|
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(char *));
|
||||||
|
printf("%s(", object->name);
|
||||||
|
for (size_t i = 0; i < object->value.argon_fn.number_of_parameters; i++) {
|
||||||
|
if(i!=0)printf(", ");
|
||||||
|
offset = pop_bytecode(translated, state);
|
||||||
|
length = pop_bytecode(translated, state);
|
||||||
|
object->value.argon_fn.parameters[i] = ar_alloc_atomic(length + 1);
|
||||||
|
memcpy(object->value.argon_fn.parameters[i], arena_get(&translated->constants, offset), length);
|
||||||
|
object->value.argon_fn.parameters[i][length] = '\0';
|
||||||
|
printf("%s", object->value.argon_fn.parameters[i]);
|
||||||
|
}
|
||||||
|
printf(") = ");
|
||||||
|
offset = pop_bytecode(translated, state);
|
||||||
|
length = pop_bytecode(translated, state);
|
||||||
|
darray_init(&object->value.argon_fn.bytecode, sizeof(uint64_t));
|
||||||
|
darray_resize(&object->value.argon_fn.bytecode, length/object->value.argon_fn.bytecode.element_size);
|
||||||
|
memcpy(object->value.argon_fn.bytecode.data, arena_get(&translated->constants, offset), length);
|
||||||
|
object->value.argon_fn.stack = stack;
|
||||||
|
fwrite(object->value.argon_fn.bytecode.data, object->value.argon_fn.bytecode.element_size, object->value.argon_fn.bytecode.size, stdout);
|
||||||
|
printf("\n");
|
||||||
|
state->registers[0]=object;
|
||||||
|
}
|
||||||
9
src/runtime/objects/functions/functions.h
Normal file
9
src/runtime/objects/functions/functions.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef FUNCTION_H
|
||||||
|
#define FUNCTION_H
|
||||||
|
#include "../object.h"
|
||||||
|
|
||||||
|
void init_function_type();
|
||||||
|
|
||||||
|
ArgonObject *load_argon_function(Translated *translated, RuntimeState *state, struct Stack stack);
|
||||||
|
|
||||||
|
#endif // FUNCTION_H
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "../runtime.h"
|
#include "../runtime.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "type/type.h"
|
||||||
|
|
||||||
ArgonObject *BASE_CLASS = NULL;
|
ArgonObject *BASE_CLASS = NULL;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ ArgonObject* init_argon_class(char*name) {
|
|||||||
object->name = name;
|
object->name = name;
|
||||||
object->type = TYPE_OBJECT;
|
object->type = TYPE_OBJECT;
|
||||||
object->self = NULL;
|
object->self = NULL;
|
||||||
object->baseObject = BASE_CLASS;
|
object->baseObject = ARGON_TYPE;
|
||||||
object->fields = createHashmap();
|
object->fields = createHashmap();
|
||||||
memset(&object->value, 0, sizeof(object->value));
|
memset(&object->value, 0, sizeof(object->value));
|
||||||
return object;
|
return object;
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
#ifndef OBJECT_H
|
#ifndef OBJECT_H
|
||||||
#define OBJECT_H
|
#define OBJECT_H
|
||||||
#include "../internals/hashmap/hashmap.h"
|
#include "../internals/hashmap/hashmap.h"
|
||||||
|
#include "../../dynamic_array/darray.h"
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "../runtime.h"
|
||||||
|
|
||||||
extern ArgonObject *BASE_CLASS;
|
extern ArgonObject *BASE_CLASS;
|
||||||
|
|
||||||
@@ -10,6 +12,12 @@ struct string_struct {
|
|||||||
char *data;
|
char *data;
|
||||||
size_t length;
|
size_t length;
|
||||||
};
|
};
|
||||||
|
struct argon_function_struct {
|
||||||
|
DArray bytecode;
|
||||||
|
struct Stack stack;
|
||||||
|
size_t number_of_parameters;
|
||||||
|
char** parameters;
|
||||||
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TYPE_NULL,
|
TYPE_NULL,
|
||||||
@@ -17,6 +25,7 @@ typedef enum {
|
|||||||
TYPE_NUMBER,
|
TYPE_NUMBER,
|
||||||
TYPE_STRING,
|
TYPE_STRING,
|
||||||
TYPE_FUNCTION,
|
TYPE_FUNCTION,
|
||||||
|
TYPE_NATIVE_FUNCTION,
|
||||||
TYPE_OBJECT, // generic user object
|
TYPE_OBJECT, // generic user object
|
||||||
} ArgonType;
|
} ArgonType;
|
||||||
|
|
||||||
@@ -31,6 +40,7 @@ struct ArgonObject {
|
|||||||
bool as_bool;
|
bool as_bool;
|
||||||
struct string_struct as_str;
|
struct string_struct as_str;
|
||||||
void *native_fn;
|
void *native_fn;
|
||||||
|
struct argon_function_struct argon_fn;
|
||||||
// others as needed
|
// others as needed
|
||||||
} value;
|
} value;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
ArgonObject *ARGON_STRING_TYPE = NULL;
|
ArgonObject *ARGON_STRING_TYPE = NULL;
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,6 @@
|
|||||||
ArgonObject *ARGON_TYPE = NULL;
|
ArgonObject *ARGON_TYPE = NULL;
|
||||||
|
|
||||||
void init_type() {
|
void init_type() {
|
||||||
ARGON_TYPE = init_argon_class("function");
|
ARGON_TYPE = init_argon_class("type");
|
||||||
|
ARGON_TYPE->baseObject = BASE_CLASS;
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "objects/object.h"
|
#include "objects/object.h"
|
||||||
#include "objects/string/string.h"
|
#include "objects/string/string.h"
|
||||||
#include "objects/type/type.h"
|
#include "objects/type/type.h"
|
||||||
|
#include "objects/functions/functions.h"
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -16,6 +17,7 @@ void init_types() {
|
|||||||
BASE_CLASS = init_argon_class("BASE_CLASS");
|
BASE_CLASS = init_argon_class("BASE_CLASS");
|
||||||
|
|
||||||
init_type();
|
init_type();
|
||||||
|
init_function_type();
|
||||||
init_null();
|
init_null();
|
||||||
init_string_type();
|
init_string_type();
|
||||||
|
|
||||||
@@ -53,6 +55,9 @@ void run_instruction(Translated *translated, RuntimeState *state, struct Stack s
|
|||||||
case OP_LOAD_CONST:
|
case OP_LOAD_CONST:
|
||||||
load_const(translated,state);
|
load_const(translated,state);
|
||||||
break;
|
break;
|
||||||
|
case OP_LOAD_FUNCTION:
|
||||||
|
load_argon_function(translated, state, stack);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ typedef struct Stack {
|
|||||||
|
|
||||||
void init_types();
|
void init_types();
|
||||||
|
|
||||||
|
uint64_t pop_bytecode(Translated *translated, RuntimeState *state);
|
||||||
|
|
||||||
void run_instruction(Translated *translated, RuntimeState *state, struct Stack stack);
|
void run_instruction(Translated *translated, RuntimeState *state, struct Stack stack);
|
||||||
|
|
||||||
void runtime(Translated translated);
|
void runtime(Translated translated);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ size_t translate_parsed_function(Translated *translated,
|
|||||||
translate_parsed(translated, parsedFunction->body);
|
translate_parsed(translated, parsedFunction->body);
|
||||||
size_t function_bytecode_offset =
|
size_t function_bytecode_offset =
|
||||||
arena_push(&translated->constants, translated->bytecode.data,
|
arena_push(&translated->constants, translated->bytecode.data,
|
||||||
translated->bytecode.size);
|
translated->bytecode.size*translated->bytecode.element_size);
|
||||||
size_t function_bytecode_length = translated->bytecode.size;
|
size_t function_bytecode_length = translated->bytecode.size;
|
||||||
translated->bytecode = main_bytecode;
|
translated->bytecode = main_bytecode;
|
||||||
darray_free(&temp_bytecode, NULL);
|
darray_free(&temp_bytecode, NULL);
|
||||||
@@ -30,6 +30,6 @@ size_t translate_parsed_function(Translated *translated,
|
|||||||
push_instruction_code(translated, strlen(*parameter_name));
|
push_instruction_code(translated, strlen(*parameter_name));
|
||||||
}
|
}
|
||||||
push_instruction_code(translated, function_bytecode_offset);
|
push_instruction_code(translated, function_bytecode_offset);
|
||||||
push_instruction_code(translated, function_bytecode_length);
|
push_instruction_code(translated, function_bytecode_length*translated->bytecode.element_size);
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user