add functions to bytecode and continuing working on runtime objects
This commit is contained in:
25
src/parser/function/function.c
Normal file
25
src/parser/function/function.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "function.h"
|
||||
#include "../../memory.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *create_parsed_function(char *name, DArray parameters,
|
||||
ParsedValue *body) {
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type=AST_FUNCTION;
|
||||
ParsedFunction *parsedFunction = checked_malloc(sizeof(ParsedFunction));
|
||||
parsedValue->data=parsedFunction;
|
||||
parsedFunction->name=strcpy(checked_malloc(strlen(name) + 1), name);
|
||||
parsedFunction->body = body;
|
||||
parsedFunction->parameters=parameters;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
void free_function(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
ParsedFunction *parsed = parsedValue->data;
|
||||
free_parsed(parsed->body);
|
||||
free(parsed->name);
|
||||
darray_free(&parsed->parameters, NULL);
|
||||
free(parsed);
|
||||
}
|
||||
17
src/parser/function/function.h
Normal file
17
src/parser/function/function.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef FUNCTION_H
|
||||
#define FUNCTION_H
|
||||
#include "../../lexer/token.h" // for Token
|
||||
#include "../parser.h"
|
||||
|
||||
typedef struct {
|
||||
char * name;
|
||||
DArray parameters;
|
||||
ParsedValue *body;
|
||||
} ParsedFunction;
|
||||
|
||||
ParsedValue *create_parsed_function(char *name, DArray parameters,
|
||||
ParsedValue *body);
|
||||
|
||||
void free_function(void *ptr);
|
||||
|
||||
#endif // FUNCTION_H
|
||||
Reference in New Issue
Block a user