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 (21)
......@@ -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.
#pragma once
#include "parser.h"
#include "heap.h"
/*typedef struct Object {
struct Value * extends;
size_t member_cnt;
SimpleEntry members [];
} Object;
typedef struct Array {
i32 length;
Value * members [];
} Array;*/
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 state_init ( ASTInterpreterState * state, Heap * heap );
void state_destroy ( ASTInterpreterState * state );
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
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#include "bc_interpreter.h"
// void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align ) {
// size_t pos = (size_t) heap -> next;
// size_t rem = pos % align;
// if ( rem )
// heap -> next = heap -> next + align - rem;
// if ( heap -> next + len >= heap -> end )
// return NULL;
// void * ret = heap -> next;
// heap -> next += len;
// return ret;
// }
//
// void * heap_alloc ( Heap * heap, size_t len ) {
// size_t pos = (size_t) heap -> next;
// size_t rem = pos % 8;
// if ( rem )
// heap -> next = heap -> next + 8 - rem;
// if ( heap -> next + len >= heap -> end )
// return NULL;
// void * ret = heap -> next;
// heap -> next += len;
// return ret;
// }
//
// void heap_init ( Heap * heap, size_t heap_size ) {
// heap -> begin = (u8*) malloc ( heap_size );
// heap -> next = heap -> begin;
// heap -> end = heap -> begin + heap_size;
// }
//
// void heap_destroy ( Heap * heap ) {
// free ( heap -> begin );
// }
//Value * make_null ( ASTInterpreterState * state ) {
// NullValue * value = heap_alloc ( state -> heap, sizeof (NullValue) );
// *value = (NullValue) { .kind = (Value) { .kind = VALUE_NULL } };
// return (Value*) value;
//}
//
//Value * make_int ( ASTInterpreterState * state, i32 val ) {
// IntValue * value = heap_alloc ( state -> heap, sizeof (IntValue) );
// *value = (IntValue) { .kind = (Value) { .kind = VALUE_INTEGER }, .integer = val };
// return (Value*) value;
//}
//
//Value * make_bool ( ASTInterpreterState * state, bool val ) {
// BoolValue * value = heap_alloc ( state -> heap, sizeof (BoolValue) );
// *value = (BoolValue) { .kind = (Value) { .kind = VALUE_BOOLEAN }, .boolean = val };
// return (Value*) value;
//}
static inline u8 read_byte ( Str str, size_t * pos ) {
return str . str [ *pos++ ];
}
void read_header ( Str input, size_t * pos ) {
u8 bytes [4];
for ( size_t i = 0; i < 4; ++i )
bytes [ i ] = read_byte ( input, pos );
Str header;
header . str = bytes;
header . len = 4;
assert ( str_eq ( header, STR ( "FML\n" ) ) );
}
Constant * read_constant_pool ( Str input, size_t * pos ) {
u8 count_bytes [ 2 ] = { read_byte ( input, pos ), read_byte ( input, pos ) };
uint16_t elements = count_bytes [ 1 ] << 8 | count_bytes [ 0 ];
printf ( "%u", elements );
return NULL;
}
Program parse_program ( Str input ) {
Program program;
// read + check header
size_t pos = 0;
read_header ( input, &pos );
// read constant pool
program . constant_pool = read_constant_pool ( input, &pos );
return program;
}
#pragma once
#include "parser.h"
#include "heap.h"
//#define READ_BYTE(str,pos) str . str [ *pos++ ]
typedef enum {
CONSTANT_INTEGER,
CONSTANT_BOOLEAN,
CONSTANT_NULL,
CONSTANT_STRING,
// VALUE_FUNCTION,
// VALUE_ARRAY,
// VALUE_OBJECT,
// VALUE_INVALID
} ConstantType;
typedef struct Constant {
ConstantType kind;
union {
i32 integer;
bool boolean;
Str string;
};
} Constant;
typedef struct Program {
Constant * constant_pool;
//uint32_t * globals;
//size_t entry_point;
} Program;
// 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 );
// Value * make_null ( ASTInterpreterState * state );
// Value * make_int ( ASTInterpreterState * state, i32 val );
// Value * make_bool ( ASTInterpreterState * state, bool val );
//inline u8 read_byte ( Str str, size_t * pos );
void read_header ( Str input, size_t * pos );
Constant * read_constant_pool ( Str input, size_t * pos );
Program parse_program ( Str input );
//int bc_interpret ( Str input, ... );
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;
}
#include "heap.h"
void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align ) {
size_t pos = (size_t) heap -> next;
size_t rem = pos % align;
if ( rem )
heap -> next = heap -> next + align - rem;
if ( heap -> next + len >= heap -> end )
return NULL;
void * ret = heap -> next;
heap -> next += len;
return ret;
}
void * heap_alloc ( Heap * heap, size_t len ) {
size_t pos = (size_t) heap -> next;
size_t rem = pos % 8;
if ( rem )
heap -> next = heap -> next + 8 - rem;
if ( heap -> next + len >= heap -> end )
return NULL;
void * ret = heap -> next;
heap -> next += len;
return ret;
}
void heap_init ( Heap * heap, size_t heap_size ) {
heap -> begin = (u8*) malloc ( heap_size );
heap -> next = heap -> begin;
heap -> end = heap -> begin + heap_size;
}
void heap_destroy ( Heap * heap ) {
free ( heap -> begin );
}
Value * make_int ( Heap * heap, i32 val ) {
IntValue * value = heap_alloc ( heap, sizeof (IntValue) );
*value = (IntValue) { .kind = (Value) { .kind = VALUE_INTEGER }, .integer = val };
return (Value*) value;
}
Value * make_bool ( Heap * heap, bool val ) {
BoolValue * value = heap_alloc ( heap, sizeof (BoolValue) );
*value = (BoolValue) { .kind = (Value) { .kind = VALUE_BOOLEAN }, .boolean = val };
return (Value*) value;
}
Value * make_function ( Heap * heap, AstFunction * function ) {
FunctionValue * value = heap_alloc ( heap, sizeof (FunctionValue) );
*value = (FunctionValue) { .kind = (Value) { .kind = VALUE_FUNCTION }, .function = function };
return (Value*) value;
}
Value * make_array ( Heap * heap, size_t len ) {
ArrayValue * value = heap_alloc ( heap, sizeof (ArrayValue) + len * sizeof (Value*) );
*value = (ArrayValue) { .kind = (Value) { .kind = VALUE_ARRAY }, .length = len };
return (Value*) value;
}
Value * make_object ( Heap * heap, size_t member_cnt ) {
ObjectValue * value = heap_alloc ( heap, sizeof (ObjectValue) + member_cnt * sizeof (SimpleEntry) );
*value = (ObjectValue) { .kind = (Value) { .kind = VALUE_OBJECT }, .member_cnt = member_cnt };
return (Value*) value;
}
Value * make_null ( Heap * heap ) {
NullValue * value = heap_alloc ( heap, sizeof (NullValue) );
*value = (NullValue) { .kind = (Value) { .kind = VALUE_NULL } };
return (Value*) value;
}
#pragma once
#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 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;
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 );
Value * make_null ( Heap * heap );
Value * make_int ( Heap * heap, i32 val );
Value * make_bool ( Heap * heap, bool val );
Value * make_function ( Heap * heap, AstFunction * function );
Value * make_array ( Heap * heap, size_t len );
Value * make_object ( Heap * heap, size_t member_cnt );
File moved
File moved