add compiler
This commit is contained in:
parent
e6348643d2
commit
e9ebefd5bb
@ -1 +0,0 @@
|
||||
#include "compiler.h"
|
@ -1,5 +0,0 @@
|
||||
#ifndef COMPILER_H
|
||||
#define COMPILER_H
|
||||
|
||||
#endif
|
||||
:wa
|
@ -1,37 +1,45 @@
|
||||
#ifndef COMMON_GENERIC_ARRAY_H
|
||||
#define COMMON_GENERIC_ARRAY_H
|
||||
#ifndef COMMON_ARRAY_H
|
||||
#define COMMON_ARRAY_H
|
||||
|
||||
#include "common/linked.h"
|
||||
#include "common/maybe_unused.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define GENERIC_ARRAY(Type, struct_name, function_prefix) \
|
||||
#define ARRAY(TYPE, STRUCT_NAME, FUNCTION_PREFIX) \
|
||||
typedef struct { \
|
||||
Type* data; \
|
||||
TYPE* data; \
|
||||
size_t length, capacity; \
|
||||
} struct_name; \
|
||||
} STRUCT_NAME; \
|
||||
\
|
||||
static inline void function_prefix##_construct(struct_name* array) \
|
||||
MAYBE_UNUSED static inline void FUNCTION_PREFIX##_construct( \
|
||||
STRUCT_NAME* array \
|
||||
) \
|
||||
{ \
|
||||
*array = (struct_name) { \
|
||||
*array = (STRUCT_NAME) { \
|
||||
.data = NULL, \
|
||||
.length = 0, \
|
||||
.capacity = 0, \
|
||||
}; \
|
||||
} \
|
||||
\
|
||||
static inline void function_prefix##_destroy(struct_name* array) \
|
||||
MAYBE_UNUSED static inline void FUNCTION_PREFIX##_destroy( \
|
||||
STRUCT_NAME* array \
|
||||
) \
|
||||
{ \
|
||||
if (array->data) \
|
||||
free(array->data); \
|
||||
} \
|
||||
\
|
||||
static inline size_t function_prefix##_length(const struct_name* array) \
|
||||
MAYBE_UNUSED static inline size_t FUNCTION_PREFIX##_length( \
|
||||
const STRUCT_NAME* array \
|
||||
) \
|
||||
{ \
|
||||
return array->length; \
|
||||
} \
|
||||
\
|
||||
static inline Type* function_prefix##_get( \
|
||||
const struct_name* array, size_t index \
|
||||
MAYBE_UNUSED static inline TYPE* FUNCTION_PREFIX##_get( \
|
||||
const STRUCT_NAME* array, size_t index \
|
||||
) \
|
||||
{ \
|
||||
if (index >= array->length) \
|
||||
@ -40,17 +48,17 @@
|
||||
return &array->data[index]; \
|
||||
} \
|
||||
\
|
||||
static inline void function_prefix##_append( \
|
||||
struct_name* array, Type value \
|
||||
MAYBE_UNUSED static inline void FUNCTION_PREFIX##_append( \
|
||||
STRUCT_NAME* array, TYPE value \
|
||||
) \
|
||||
{ \
|
||||
if (array->data == NULL) { \
|
||||
array->capacity = 8; \
|
||||
array->data = malloc(sizeof(Type) * array->capacity); \
|
||||
array->data = malloc(sizeof(TYPE) * array->capacity); \
|
||||
} else if (array->length == array->capacity) { \
|
||||
array->capacity *= 2; \
|
||||
array->data \
|
||||
= realloc(array->data, sizeof(Type) * array->capacity); \
|
||||
= realloc(array->data, sizeof(TYPE) * array->capacity); \
|
||||
} \
|
||||
array->data[array->length] = value; \
|
||||
array->length++; \
|
15
include/common/linked.h
Normal file
15
include/common/linked.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef COMMON_LINKED_H
|
||||
#define COMMON_LINKED_H
|
||||
|
||||
/*
|
||||
* ISO C requires at least one declaration
|
||||
* clangd will therefore generate warnings for header files
|
||||
* containing only macros or nothing at all.
|
||||
*
|
||||
* To mitigate this, include this file
|
||||
* containing this declaration
|
||||
* which removes the warning.
|
||||
*/
|
||||
int common_linked_declaration(void);
|
||||
|
||||
#endif
|
18
include/common/maybe_unused.h
Normal file
18
include/common/maybe_unused.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef COMMON_MAYBE_UNUSED_H
|
||||
#define COMMON_MAYBE_UNUSED_H
|
||||
|
||||
#include "common/linked.h"
|
||||
|
||||
#if defined __STDC__ && __STDC__ == 1 && defined __STDC_VERSION__ \
|
||||
&& __STDC_VERSION__ >= 202311L
|
||||
#define MAYBE_UNUSED [[maybe_unused]]
|
||||
#warning "MAYBE_UNUSED macro can be replaced by C23 [[maybe_unused]]"
|
||||
#elif defined __clang__ || defined __GNUC__
|
||||
#define MAYBE_UNUSED __attribute__((unused))
|
||||
#elif defined _MSC_VER
|
||||
#define MAYBE_UNUSED __pragma(warning(suppress : 4505))
|
||||
#else
|
||||
#warning "cannot define MAYBE_UNUSED"
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,36 +1,39 @@
|
||||
#ifndef COMMON_RESULT_H
|
||||
#define COMMON_RESULT_H
|
||||
|
||||
#include "common/linked.h"
|
||||
#include "common/maybe_unused.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define RESULT(Value, Error, struct_name) \
|
||||
#define RESULT(VALUE, ERROR, STRUCT_NAME) \
|
||||
typedef struct { \
|
||||
bool ok; \
|
||||
union { \
|
||||
Value value; \
|
||||
Error error; \
|
||||
VALUE value; \
|
||||
ERROR error; \
|
||||
}; \
|
||||
} struct_name;
|
||||
} STRUCT_NAME;
|
||||
|
||||
#define RESULT_CTORS(Value, Error, struct_name, function_prefix) \
|
||||
static inline struct_name function_prefix##_ok(Value value) \
|
||||
#define RESULT_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX) \
|
||||
MAYBE_UNUSED static inline STRUCT_NAME FUNCTION_PREFIX##_ok(VALUE value) \
|
||||
{ \
|
||||
return (struct_name) { \
|
||||
return (STRUCT_NAME) { \
|
||||
.ok = true, \
|
||||
.value = value, \
|
||||
}; \
|
||||
} \
|
||||
\
|
||||
static inline struct_name function_prefix##_error(Error error) \
|
||||
MAYBE_UNUSED static inline STRUCT_NAME FUNCTION_PREFIX##_error(ERROR error \
|
||||
) \
|
||||
{ \
|
||||
return (struct_name) { \
|
||||
return (STRUCT_NAME) { \
|
||||
.ok = false, \
|
||||
.error = error, \
|
||||
}; \
|
||||
}
|
||||
|
||||
#define RESULT_WITH_CTORS(Value, Error, struct_name, function_prefix) \
|
||||
RESULT(Value, Error, struct_name) \
|
||||
RESULT_CTORS(Value, Error, struct_name, function_prefix)
|
||||
#define RESULT_WITH_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX) \
|
||||
RESULT(VALUE, ERROR, STRUCT_NAME) \
|
||||
RESULT_CTORS(VALUE, ERROR, STRUCT_NAME, FUNCTION_PREFIX)
|
||||
|
||||
#endif
|
||||
|
@ -1,7 +1,8 @@
|
||||
#ifndef COMMON_STRING_H
|
||||
#define COMMON_STRING_H
|
||||
|
||||
#include "common/generic_array.h"
|
||||
#include "common/array.h"
|
||||
#include "common/maybe_unused.h"
|
||||
#include "common/result.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
@ -17,7 +18,8 @@ typedef struct {
|
||||
size_t length;
|
||||
} HeapString;
|
||||
|
||||
static inline HeapString heapstring_from(const char* text, size_t length)
|
||||
MAYBE_UNUSED static inline HeapString
|
||||
heapstring_from(const char* text, size_t length)
|
||||
{
|
||||
char* allocated = malloc(sizeof(char) * length + 1);
|
||||
strncpy(allocated, text, length);
|
||||
@ -28,23 +30,23 @@ static inline HeapString heapstring_from(const char* text, size_t length)
|
||||
return string;
|
||||
}
|
||||
|
||||
static inline HeapString heapstring_from_cstring(const char* text)
|
||||
MAYBE_UNUSED static inline HeapString heapstring_from_cstring(const char* text)
|
||||
{
|
||||
return heapstring_from(text, strlen(text));
|
||||
}
|
||||
|
||||
static inline void heapstring_destroy(HeapString* string)
|
||||
MAYBE_UNUSED static inline void heapstring_destroy(HeapString* string)
|
||||
{
|
||||
if (string->data)
|
||||
free(string->data);
|
||||
}
|
||||
|
||||
static inline HeapString heapstring_clone(const HeapString* source)
|
||||
MAYBE_UNUSED static inline HeapString heapstring_clone(const HeapString* source)
|
||||
{
|
||||
return heapstring_from(source->data, source->length);
|
||||
}
|
||||
|
||||
GENERIC_ARRAY(char, StringBuilder, stringbuilder)
|
||||
ARRAY(char, StringBuilder, stringbuilder)
|
||||
HeapString stringbuilder_build(StringBuilder* builder);
|
||||
|
||||
RESULT(HeapString, HeapString, UnescapeStringResult)
|
||||
|
@ -1,33 +1,34 @@
|
||||
#ifndef COMMON_STRING_ARRAY_H
|
||||
#define COMMON_STRING_ARRAY_H
|
||||
|
||||
#include "common/generic_array.h"
|
||||
#include "common/array.h"
|
||||
#include "common/maybe_unused.h"
|
||||
#include "common/string.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
GENERIC_ARRAY(StringView, StringViewArray, stringview_array)
|
||||
ARRAY(StringView, StringViewArray, stringview_array)
|
||||
|
||||
static inline StringViewArray* stringview_array_new(void)
|
||||
MAYBE_UNUSED static inline StringViewArray* stringview_array_new(void)
|
||||
{
|
||||
StringViewArray* array = malloc(sizeof(StringViewArray));
|
||||
stringview_array_construct(array);
|
||||
return array;
|
||||
}
|
||||
static inline void stringview_array_delete(StringViewArray* array)
|
||||
MAYBE_UNUSED static inline void stringview_array_delete(StringViewArray* array)
|
||||
{
|
||||
stringview_array_destroy(array);
|
||||
free(array);
|
||||
}
|
||||
|
||||
GENERIC_ARRAY(HeapString, HeapStringArray, heapstring_array)
|
||||
ARRAY(HeapString, HeapStringArray, heapstring_array)
|
||||
|
||||
static inline HeapStringArray* heapstring_array_new(void)
|
||||
MAYBE_UNUSED static inline HeapStringArray* heapstring_array_new(void)
|
||||
{
|
||||
HeapStringArray* array = malloc(sizeof(HeapStringArray));
|
||||
heapstring_array_construct(array);
|
||||
return array;
|
||||
}
|
||||
static inline void heapstring_array_delete(HeapStringArray* array)
|
||||
MAYBE_UNUSED static inline void heapstring_array_delete(HeapStringArray* array)
|
||||
{
|
||||
heapstring_array_destroy(array);
|
||||
free(array);
|
||||
|
@ -1,12 +1,13 @@
|
||||
#ifndef COMMON_STRINGMAP_H
|
||||
#define COMMON_STRINGMAP_H
|
||||
|
||||
#include "common/maybe_unused.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// https://stackoverflow.com/questions/7666509/hash-function-for-string
|
||||
// http://www.cse.yorku.ca/~oz/hash.html
|
||||
static inline size_t
|
||||
MAYBE_UNUSED static inline size_t
|
||||
common_string_hash_djb2(const unsigned char* value, size_t length)
|
||||
{
|
||||
size_t hash = 5381;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef SCIRPT_AST_H
|
||||
#define SCIRPT_AST_H
|
||||
|
||||
#include "common/generic_array.h"
|
||||
#include "common/array.h"
|
||||
#include "common/string.h"
|
||||
#include "common/string_array.h"
|
||||
#include "scirpt/position.h"
|
||||
@ -39,14 +39,14 @@ typedef enum {
|
||||
|
||||
typedef struct ScirptExpr ScirptExpr;
|
||||
|
||||
GENERIC_ARRAY(ScirptExpr*, ScirptExprArray, scirpt_expr_array)
|
||||
ARRAY(ScirptExpr*, ScirptExprArray, scirpt_expr_array)
|
||||
|
||||
typedef struct {
|
||||
HeapString key;
|
||||
ScirptExpr* value;
|
||||
} ScirptExprObjectEntry;
|
||||
|
||||
GENERIC_ARRAY(
|
||||
ARRAY(
|
||||
ScirptExprObjectEntry,
|
||||
ScirptExprObjectEntryArray,
|
||||
scirpt_expr_object_entry_array
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef BYTECODE_H
|
||||
#define BYTECODE_H
|
||||
#ifndef SCIRPT_BYTECODE_H
|
||||
#define SCIRPT_BYTECODE_H
|
||||
|
||||
#include "common/array.h"
|
||||
#include "common/string.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@ -32,4 +33,6 @@ typedef struct {
|
||||
};
|
||||
} ScirptInstruction;
|
||||
|
||||
ARRAY(ScirptInstruction, ScirptInstructionArray, scirpt_instruction_array)
|
||||
|
||||
#endif
|
6
include/scirpt/compiler.h
Normal file
6
include/scirpt/compiler.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef SCIRPT_COMPILER_H
|
||||
#define SCIRPT_COMPILER_H
|
||||
|
||||
#include "scirpt/bytecode.h"
|
||||
|
||||
#endif
|
@ -1,7 +1,7 @@
|
||||
#ifndef SCIRPT_PARSER_H
|
||||
#define SCIRPT_PARSER_H
|
||||
|
||||
#include "common/generic_array.h"
|
||||
#include "common/array.h"
|
||||
#include "common/string.h"
|
||||
#include "scirpt/ast.h"
|
||||
#include "scirpt/lexer.h"
|
||||
@ -14,9 +14,7 @@ typedef struct {
|
||||
|
||||
void scirpt_parser_error_destroy(ScirptParserError* error);
|
||||
|
||||
GENERIC_ARRAY(
|
||||
ScirptParserError, ScirptParserErrorArray, scirpt_parser_error_array
|
||||
)
|
||||
ARRAY(ScirptParserError, ScirptParserErrorArray, scirpt_parser_error_array)
|
||||
|
||||
typedef struct ScirptParser ScirptParser;
|
||||
|
||||
|
3
scirpt/compiler.c
Normal file
3
scirpt/compiler.c
Normal file
@ -0,0 +1,3 @@
|
||||
#include "scirpt/compiler.h"
|
||||
#include "compiler.h"
|
||||
#include "scirpt/bytecode.h"
|
10
scirpt/compiler.h
Normal file
10
scirpt/compiler.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef COMPILER_H
|
||||
#define COMPILER_H
|
||||
|
||||
#include "scirpt/bytecode.h"
|
||||
#include "scirpt/compiler.h"
|
||||
|
||||
typedef struct {
|
||||
} ScirptCompiler;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user