Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • vlasami6/ni-run-template
  • chybijan/ni-run
  • ruzict16/FML
  • rozhovoj/ni-run-template
  • skrabmir/cfml
  • plodeada/ni-run-plodeada
  • stepam38/ni-run
  • hrncikar/ni-run
  • balikvo1/ni-run
9 results
Show changes
Commits on Source (19)
......@@ -2,8 +2,25 @@ image: $CI_REGISTRY/vlasami6/fmltest:master
 
test:
script:
- meson setup build
- meson compile -C build
- cppcheck --error-exitcode=1 *.c
- env FML="$(readlink -f ./build/fml)" FML_REF_BC_INT=/cfml/fml /FMLtest/suite ast_interpret
hello_world.fml
- make
#- cppcheck --error-exitcode=1 *.c
- env FML="$(readlink -f ./fml)" FML_REF_BC_INT=/cfml/fml /FMLtest/suite ast_interpret
$(/FMLtest/suite show fml)
#hello_world
#literals
#variables
#conditionals
#loops
#functions
#function_value
#builtins
#arrays
#iterator
#fibonacci
#fizzbuzz_fun
#fizzbuzz_loop
#roman
#stack
#brainfuck
#langtons_ant
#sudoku
CC=gcc
CFLAGS=-Wall -pedantic -Wextra -g #-fsanitize=address,undefined
LFLAGS=
OUTPUT=fml
SRC_DIR=./src
BUILD_DIR=./build
.PHONY: init fml
all: init fml
fml: $(BUILD_DIR)/arena.o $(BUILD_DIR)/parser.o $(BUILD_DIR)/ast_interpreter.o $(SRC_DIR)/fml.c #$(BUILD_DIR)/%.o
$(CC) $(CFLAGS) $(LFLAGS) $? -o $(OUTPUT)
%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/ast_interpreter.o: $(SRC_DIR)/parser.o $(SRC_DIR)/ast_interpreter.c
$(CC) $(CFLAGS) -c $? -o $@
$(BUILD_DIR)/parser.o: $(SRC_DIR)/parser.c
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/arena.o: $(SRC_DIR)/arena.c
$(CC) $(CFLAGS) -c $< -o $@
init:
mkdir -p build
File moved
File moved
This diff is collapsed.
#include "parser.h"
typedef enum {
VALUE_INTEGER,
VALUE_BOOLEAN,
VALUE_NULL,
VALUE_FUNCTION,
VALUE_ARRAY,
VALUE_OBJECT,
VALUE_INVALID
} ValueType;
typedef struct Heap {
u8 * begin;
u8 * next;
u8 * end;
} Heap;
typedef struct Value {
ValueType kind;
} Value;
typedef struct SimpleEntry {
Str name;
Value * value;
} SimpleEntry;
typedef struct Object {
struct Value * extends;
size_t member_cnt;
SimpleEntry members [];
} Object;
typedef struct Array {
i32 length;
Value * members [];
} Array;
typedef struct NullValue {
Value kind;
} NullValue;
typedef struct IntValue {
Value kind;
i32 integer;
} IntValue;
typedef struct BoolValue {
Value kind;
bool boolean;
} BoolValue;
typedef struct FunctionValue {
Value kind;
AstFunction * function;
} FunctionValue;
typedef struct ObjectValue {
Value kind;
Value * extends;
size_t member_cnt;
SimpleEntry members [];
} ObjectValue;
typedef struct ArrayValue {
Value kind;
i32 length;
Value * elements [];
} ArrayValue;
typedef struct EnvironmentEntry {
struct EnvironmentEntry * next;
Str name;
Value * value;
} EnvironmentEntry;
typedef struct Environment {
struct Environment * prev;
EnvironmentEntry * start;
} Environment;
typedef struct ASTInterpreterState {
Heap * heap;
Environment global_env;
Environment * current_env;
} ASTInterpreterState;
bool value_to_bool ( Value * value );
void env_push ( ASTInterpreterState * state );
void env_pop ( ASTInterpreterState * state );
Value * env_get ( ASTInterpreterState * state, Str name );
void env_put ( ASTInterpreterState * state, Str name, Value * value );
void env_def ( ASTInterpreterState * state, Str name, Value * value );
void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align );
void * heap_alloc ( Heap * heap, size_t len );
void heap_init ( Heap * heap, size_t heap_size );
void heap_destroy ( Heap * heap );
void state_init ( ASTInterpreterState * state, Heap * heap );
void state_destroy ( ASTInterpreterState * state );
Value * make_null ( ASTInterpreterState * state );
Value * make_int ( ASTInterpreterState * state, i32 val );
Value * make_bool ( ASTInterpreterState * state, bool val );
Value * make_function ( ASTInterpreterState * state, AstFunction * function );
Value * make_array ( ASTInterpreterState * state, size_t len );
Value * make_object ( ASTInterpreterState * state, size_t member_cnt );
Value * get_base ( Value * object );
Value ** get_object_field (Value * object, Str name );
Value * try_operator ( ASTInterpreterState * state, Value * object, Value ** arguments, size_t argc, Str * name );
Value * function_call ( ASTInterpreterState * state, Value * callee, bool is_function, Ast ** arguments, size_t argc, Str * name );
void print_value ( Value * value );
void fml_print ( Str format, Value ** args, size_t argc );
Value * evaluate ( ASTInterpreterState * state, Ast * ast );
int compare_entry ( const void * a, const void * b );
\ No newline at end of file
File moved
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <ctype.h>
#include "arena.h"
#include "parser.h"
#include "ast_interpreter.h"
#define AST_INTERPRETER_COMMAND "ast_interpret"
#define RUN "run"
#define HEAP_SIZE_COMMAND "--heap-size"
#define HEAP_LOG_COMMAND "--heap-log"
#define DEFAULT_HEAP_SIZE 512
int main ( int argc, char **argv ) {
if ( argc < 2 ) {
fprintf( stderr, "Input error: expected at least one argument\n" );
return 1;
}
size_t heap_size = DEFAULT_HEAP_SIZE;
char * log_file = NULL;
u8 flag = 0;
u8 ast_interpret = 0;
size_t len;
int f = -1;
for ( int arg = 1; arg < argc; ++arg ) {
len = strlen ( argv [ arg ] );
for ( size_t j = 0; j < len; j++ )
argv [ arg ] [ j ] = tolower ( argv [ arg ] [ j ] );
if ( len == strlen ( AST_INTERPRETER_COMMAND ) && strncmp ( argv [ arg ], AST_INTERPRETER_COMMAND, len ) == 0 ) {
ast_interpret = 1;
f = ++arg;
} else if ( len == strlen ( RUN ) && strncmp ( argv [ arg ], RUN, len ) == 0 ) {
ast_interpret = 1;
for ( size_t opt = 0; opt < 2; ++opt ) {
len = strlen ( argv [ ++arg ] );
if ( len == strlen ( HEAP_SIZE_COMMAND ) && strncmp ( argv [ arg ], HEAP_SIZE_COMMAND, len ) == 0 ) {
heap_size = atoi ( argv [ ++arg ] );
if ( heap_size == 0 ) {
fprintf( stderr, "Input error: invalid heap size %s.\n", argv [ arg ] );
flag = 2;
}
} else if ( len == strlen ( HEAP_LOG_COMMAND ) && strncmp ( argv [ arg ], HEAP_LOG_COMMAND, len ) == 0 ) {
log_file = argv [ ++arg ];
}
}
f = ++arg;
} else {
fprintf( stderr, "Input error: unknown argument %s.\n", argv [ arg ] );
flag = 1;
}
}
if ( flag || f <= 0 )
return 1;
FILE * fp = fopen ( argv [ f ], "r" );
(void) log_file;
if ( ! fp ) {
fprintf ( stderr, "Input error: file does not exists.\n" );
return 1;
}
if ( fseek ( fp, 0, SEEK_END ) ) {
fprintf ( stderr, "Input error: could not advance to the end of the file.\n" );
return 1;
}
int file_len = ftell ( fp );
if ( file_len < 0 ) {
fprintf ( stderr, "Input error: could not count the size of the file.\n" );
return 1;
}
size_t flen = file_len;
rewind ( fp );
u8 * buffer = malloc ( flen );
if ( fread ( buffer, 1, flen, fp ) != flen ) {
fprintf ( stderr, "Input error: could not read to the file.\n" );
return 1;
}
fclose ( fp );
Arena arena;
arena_init( &arena );
Ast *ast = parse_src ( &arena, (Str) { .str = buffer, .len = flen } );
if ( ast == NULL ) {
fprintf ( stderr, "Failed to parse source\n" );
arena_destroy ( &arena );
return 1;
}
if ( ast_interpret ) {
ASTInterpreterState state;
Heap heap;
heap_init ( &heap, heap_size * 1024 * 1024 );
state_init ( &state, &heap );
evaluate ( &state, ast );
state_destroy ( &state );
}
free ( buffer );
arena_destroy( &arena );
return 0;
}
File moved
File moved