Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output: main.o lex.o grammar.o create.o string.o compiler.o codegen.o opcode.o error.o debug.o wchar.o
output: main.o lex.o grammar.o create.o string.o compiler.o builtin.o fix_tree.o codegen.o opcode.o error.o debug.o wchar.o
$(CC) -o bin/main $^
main.o : main/main.c
$(CC) -c main/main.c
Expand All @@ -8,6 +8,10 @@ opcode.o : compiler/opcode.c
$(CC) -c $< -o opcode.o
compiler.o : compiler/compiler.c
$(CC) -c $< -o compiler.o
fix_tree.o : compiler/fix_tree.c
$(CC) -c $< -o fix_tree.o
builtin.o : builtin/builtin.c
$(CC) -c $< -o builtin.o
create.o : compiler/create.c
$(CC) -c $< -o create.o
grammar.o : compiler/grammar.y
Expand Down
24 changes: 14 additions & 10 deletions builtin/builtin.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#include <string.h>
#include "../include/share.h"
#include "../compiler/summoner.h"

typedef struct
{
const char *name;
BasicType *parameters;
int parameter_count;
BasicType return_type;
const char *op_codes;
} BuiltinFun;

static BasicType sha3_params[] = {HEX_TYPE};
static BasicType sha256_params[] = {HEX_TYPE};
static BasicType abs_params[] = {INT_TYPE};
Expand All @@ -22,7 +14,7 @@ static BasicType above_params[] = {INT_TYPE, BOOLEAN_TYPE};
static BasicType lock_params[] = {AMOUNT_TYPE, ASSET_TYPE, HEX_TYPE, HEX_TYPE};
static BasicType verify_params[] = {BOOLEAN_TYPE};

BuiltinFun builtin_funs[] = {
BuiltinFun builtin_func_list[] = {
{"sha3", sha3_params, ARRAY_SIZE(sha3_params), HASH_TYPE, "SHA3"},
{"sha256", sha256_params, ARRAY_SIZE(sha256_params), HASH_TYPE, "SHA256"},
{"abs", abs_params, ARRAY_SIZE(abs_params), INT_TYPE, "ABS"},
Expand All @@ -35,3 +27,15 @@ BuiltinFun builtin_funs[] = {
{"lock", lock_params, ARRAY_SIZE(lock_params), VOID_TYPE, ""},
{"verify", verify_params, ARRAY_SIZE(verify_params), VOID_TYPE, "VERIFY"},
};

BuiltinFun *search_builtin_function(char *name)
{
for (int i = 0; i < ARRAY_SIZE(builtin_func_list); i++)
{
if (!strcmp(name, builtin_func_list[i].name))
{
return &builtin_func_list[i];
}
}
return NULL;
}
2 changes: 1 addition & 1 deletion compiler/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ generate_double_expression(SVM_Executable *cf, Expression *expr,
}

static void
generate_identifier(char *ident, OpcodeBuf *ob)
generate_identifier(IdentifierExpression *identifier_expr, OpcodeBuf *ob)
{
}

Expand Down
22 changes: 13 additions & 9 deletions compiler/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ Expression *alloc_double_expression(double value)
Expression *alloc_identifier_expression(char *identifier)
{
Expression *expr = alloc_expression(IDENTIFIER_EXPRESSION);
expr->u.identifier = identifier;
IdentifierExpression *identifier_expr = (IdentifierExpression *)malloc(sizeof(IdentifierExpression));
identifier_expr->kind = 0;
identifier_expr->name = NULL;
expr->u.identifier = identifier_expr;
return expr;
}

Expand Down Expand Up @@ -69,11 +72,11 @@ Expression *alloc_binary_expression(ExpressionKind kind, Expression *left, Expre
return expr;
}

Expression *alloc_func_call_expression(char *identifier, ArgumentList *argument_list)
Expression *alloc_func_call_expression(Expression *function, ArgumentList *argument_list)
{
Expression *expr = alloc_expression(FUNC_CALL_EXPRESSION);
FuncCallExpression *func_call_e = (FuncCallExpression *)malloc(sizeof(FuncCallExpression));
func_call_e->identifier = identifier;
func_call_e->function = function;
func_call_e->argument_list = argument_list;
expr->u.func_call_expression = func_call_e;
return expr;
Expand Down Expand Up @@ -101,19 +104,19 @@ Statement *alloc_stmt(StatementKind kind)
return stmt;
}

Statement *alloc_assign_stmt(char *variable, Expression *operand)
Statement *alloc_assign_stmt(Expression *identifier_expr, Expression *operand)
{
Statement *stmt = alloc_stmt(ASSIGN_STATEMENT);
AssignStatement *assign_s = (AssignStatement *)malloc(sizeof(AssignStatement));
assign_s->variable = variable;
assign_s->left = identifier_expr;
assign_s->operand = operand;
stmt->u.assign_s = assign_s;
return stmt;
}

Statement *alloc_compound_assign_stmt(char *variable, ExpressionKind kind, Expression *operand)
Statement *alloc_compound_assign_stmt(Expression *identifier_expr, ExpressionKind kind, Expression *operand)
{
return alloc_assign_stmt(variable, alloc_binary_expression(kind, alloc_identifier_expression(variable), operand));
return alloc_assign_stmt(identifier_expr, alloc_binary_expression(kind, identifier_expr, operand));
}

Statement *alloc_if_stmt(Expression *condition, Block *then_block, Elseif *elseif_list, Block *else_block)
Expand Down Expand Up @@ -238,11 +241,10 @@ Block *alloc_block(StatementList *list)
return block;
}

TypeSpecifier *alloc_type_specifier(BasicType type, char *identifier)
TypeSpecifier *alloc_type_specifier(BasicType type)
{
TypeSpecifier *type_s = (TypeSpecifier *)malloc(sizeof(TypeSpecifier));
type_s->basic_type = type;
type_s->identifier = identifier;
return type_s;
}

Expand Down Expand Up @@ -282,6 +284,8 @@ Definition *alloc_func_definition(char *name, ParameterList *parameters, TypeSpe
func_d->parameters = parameters;
func_d->return_type = return_type;
func_d->block = block;
func_d->local_variable_count = 0;
func_d->local_variable = NULL;
func_d->next = NULL;

Definition *definition = alloc_definition(FUNC_DEFINITION);
Expand Down
5 changes: 3 additions & 2 deletions compiler/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ static ErrorDefinition dkc_error_message_format[] = {
{"在C样式的注释中终止了文件。"},
{"在字符串字面量中终止了文件。"},
{"字符字面量中包含了2个以上的字符。"},
{"dummy"}
};
{"类型转换不匹配。"},
{"函数调用类型不匹配。"},
{"dummy"}};

typedef struct
{
Expand Down
2 changes: 2 additions & 0 deletions compiler/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ typedef enum
EOF_IN_C_COMMENT_ERR,
EOF_IN_STRING_LITERAL_ERR,
TOO_LONG_CHARACTER_LITERAL_ERR,
TYPE_CAST_MISMATH_ERR,
FUNCTION_CALL_TYPE_MISMATCH_ERR,
COMPILE_ERROR_COUNT_PLUS_1,
} CompileError;

Expand Down
Loading