Skip to content

Commit

Permalink
Algo-tests, fix break statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Dec 30, 2024
1 parent 6ecf5d3 commit 2ce8390
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 45 deletions.
29 changes: 24 additions & 5 deletions altlib/stdio.al
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func ^.atoi_(str #1C) -> #I {
return x
}

func ^.freadc_(fd #I) -> #C {
def c := '\0'
return if (posix_read(fd, c&, 1) = 0) '\0' else c
}

func ^.freads_(fd #I, dst #1C) -> #I {
def i := 0
def dsti := dst as #I
Expand All @@ -80,17 +85,31 @@ func ^.freads_(fd #I, dst #1C) -> #I {
return i
}

func ^.freadi_(fd #I, dst #1C) -> #I {
eval freads_(fd, dst)
return atoi_(dst)
func ^.freadi_(fd #I) -> #I {
def x := 0
def start := 0
return while (1) {
def c := freadc_(fd)
eval if ((c = ' ' or c = '\n') and start = 0) { continue }
start := 1
eval if (not (c >= '0' and c <= '9')) { break x }
def d := (c - '0') as #I
x := x * 10 + d
}
else 0
}

func ^.reads_(dst #1C) -> #I {
def STDIN := 0
return freads_(STDIN, dst)
}

func ^.readi_(dst #1C) -> #I {
func ^.readc_() -> #C {
def STDIN := 0
return freadc_(STDIN)
}

func ^.readi_() -> #I {
def STDIN := 0
return freadi_(STDIN, dst)
return freadi_(STDIN)
}
24 changes: 15 additions & 9 deletions compiler/src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ void compile_break(struct Node *node, struct Break *this, struct CPContext *cont
error_semantic("Break values types do not equal", node);
}
}

int tsize = type_size(_type, context);
const char *dst = _concat3("[rbp + ", _itoa(label_info->sf_pos - align_to_word(tsize)), "]");
const char *src = _concat3("[rbp + ", _itoa(context->sf_pos - align_to_word(tsize)), "]");
compile_memcpy(dst, src, tsize, context);

_fputsi(context->fd_text, "add rsp, ", label_info->sf_pos - context->sf_pos, "\n");
_fputs3(context->fd_text, "jmp ", label_info->name_end, "\n");
}
Expand Down Expand Up @@ -953,7 +959,7 @@ struct TypeNode *compile_and(struct Node *node, struct BinaryOperator *this, str
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 0\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_or(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -972,7 +978,7 @@ struct TypeNode *compile_or(struct Node *node, struct BinaryOperator *this, stru
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_not(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -988,7 +994,7 @@ struct TypeNode *compile_not(struct Node *node, struct BinaryOperator *this, str
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_bitwise_and(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand Down Expand Up @@ -1097,7 +1103,7 @@ struct TypeNode *compile_less(struct Node *node, struct BinaryOperator *this, st
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_greater(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -1112,7 +1118,7 @@ struct TypeNode *compile_greater(struct Node *node, struct BinaryOperator *this,
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_equal(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -1127,7 +1133,7 @@ struct TypeNode *compile_equal(struct Node *node, struct BinaryOperator *this, s
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_less_equal(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -1142,7 +1148,7 @@ struct TypeNode *compile_less_equal(struct Node *node, struct BinaryOperator *th
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_greater_equal(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -1157,7 +1163,7 @@ struct TypeNode *compile_greater_equal(struct Node *node, struct BinaryOperator
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_not_equal(struct Node *node, struct BinaryOperator *this, struct CPContext *context) {
Expand All @@ -1172,7 +1178,7 @@ struct TypeNode *compile_not_equal(struct Node *node, struct BinaryOperator *thi
_fputsi(context->fd_text, "_L", idx, ":\n");
_fputs(context->fd_text, "mov qword [rsp - 8], 1\n");
_fputsi(context->fd_text, "_L", idx + 1, ":\n");
return _type;
return context->node_int;
}

struct TypeNode *compile_node(struct Node *node, struct CPContext *context) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ struct Node *syntax_process_expression(struct TokenStream *ts, struct Settings *
CurrentState = State_ParenthesisOpen;
}
else if (tokenstream_get(ts).type == TokenParenthesisClose) {
if (CurrentState != State_Identifier) {
if (CurrentState != State_Identifier && CurrentState != State_ParenthesisClose) {
error_syntax("Unexpected ) in expression", tokenstream_get(ts));
}
while (vsize(&operations) != NULL && ((struct Token*)vback(&operations))->type != TokenParenthesisOpen) {
Expand Down
14 changes: 5 additions & 9 deletions test/algo/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
ALFLAGS=-i altlib ../../altlib/
LDFLAGS=-z noexecstack

SRCS_AL := $(wildcard *.al)
EXES_AL := $(patsubst %.al, $(BUILD_DIR)/test/algo/%, $(SRCS_AL))
SRCS_AL := $(wildcard */main.al)
DIRS := $(patsubst %/main.al, $(BUILD_DIR)/test/algo/%, $(SRCS_AL))

OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)
OBJS_ALTLIB := $(wildcard $(BUILD_DIR)/altlib/*.o)
all: $(DIRS)

all: $(EXES_AL)

$(BUILD_DIR)/test/algo/%: %.al make_dir
$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $(patsubst %, %.o, $@)
ld $(LDFLAGS) -o $@ $(patsubst %, %.o, $@) $(OBJS_STDLIB) $(OBJS_ALTLIB)
$(BUILD_DIR)/test/algo/%: % make_dir
$(MAKE) -C $< BUILD_DIR=$(BUILD_DIR) WORKING_DIR=$(BUILD_DIR)/test/algo/$<

make_dir:
mkdir -p $(BUILD_DIR)/test/algo
Expand Down
21 changes: 0 additions & 21 deletions test/algo/a_plus_b.al

This file was deleted.

28 changes: 28 additions & 0 deletions test/algo/a_plus_b/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ALFLAGS=-i altlib ../../../altlib/
LDFLAGS=-z noexecstack

SRCS_AL := $(wildcard *.al)
EXES_AL := $(patsubst %.al, $(BUILD_DIR)/test/integration/%, $(SRCS_AL))

TESTS_IN := $(wildcard in/*)
TESTS_OUT := $(wildcard out/*)
TESTS_OUT2 := $(patsubst out/%, $(WORKING_DIR)/out/%, $(TESTS_OUT))

OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)
OBJS_ALTLIB := $(wildcard $(BUILD_DIR)/altlib/*.o)

all: $(WORKING_DIR)/main $(TESTS_OUT2)

$(WORKING_DIR)/main: main.al make_dir
$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $(patsubst %, %.o, $@)
ld $(LDFLAGS) -o $@ $(patsubst %, %.o, $@) $(OBJS_STDLIB) $(OBJS_ALTLIB)

$(WORKING_DIR)/out/%: in/% $(WORKING_DIR)/main
$(WORKING_DIR)/main < $< > $@
diff $@ $(patsubst in/%, out/%, $<) || (echo "Test failed"; exit 1)

make_dir:
mkdir -p $(WORKING_DIR)/out

clean:
rm -r $(BUILD_DIR)
1 change: 1 addition & 0 deletions test/algo/a_plus_b/in/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 3
1 change: 1 addition & 0 deletions test/algo/a_plus_b/in/2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
43 58
7 changes: 7 additions & 0 deletions test/algo/a_plus_b/in/3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


5943



49789
7 changes: 7 additions & 0 deletions test/algo/a_plus_b/main.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include altlib."posix.al"
include altlib."stdio.al"

func ^._start() -> #V {
eval puti_(readi_() + readi_())
eval posix_exit(0)
}
1 change: 1 addition & 0 deletions test/algo/a_plus_b/out/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
1 change: 1 addition & 0 deletions test/algo/a_plus_b/out/2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
101
1 change: 1 addition & 0 deletions test/algo/a_plus_b/out/3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
55732
28 changes: 28 additions & 0 deletions test/algo/read_write_string/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ALFLAGS=-i altlib ../../../altlib/
LDFLAGS=-z noexecstack

SRCS_AL := $(wildcard *.al)
EXES_AL := $(patsubst %.al, $(BUILD_DIR)/test/integration/%, $(SRCS_AL))

TESTS_IN := $(wildcard in/*)
TESTS_OUT := $(wildcard out/*)
TESTS_OUT2 := $(patsubst out/%, $(WORKING_DIR)/out/%, $(TESTS_OUT))

OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)
OBJS_ALTLIB := $(wildcard $(BUILD_DIR)/altlib/*.o)

all: $(WORKING_DIR)/main $(TESTS_OUT2)

$(WORKING_DIR)/main: main.al make_dir
$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $(patsubst %, %.o, $@)
ld $(LDFLAGS) -o $@ $(patsubst %, %.o, $@) $(OBJS_STDLIB) $(OBJS_ALTLIB)

$(WORKING_DIR)/out/%: in/% $(WORKING_DIR)/main
$(WORKING_DIR)/main < $< > $@
diff $@ $(patsubst in/%, out/%, $<) || (echo "Test failed"; exit 1)

make_dir:
mkdir -p $(WORKING_DIR)/out

clean:
rm -r $(BUILD_DIR)
1 change: 1 addition & 0 deletions test/algo/read_write_string/in/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
2 changes: 2 additions & 0 deletions test/algo/read_write_string/in/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

abacaba
File renamed without changes.
1 change: 1 addition & 0 deletions test/algo/read_write_string/out/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
1 change: 1 addition & 0 deletions test/algo/read_write_string/out/2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abacaba

0 comments on commit 2ce8390

Please sign in to comment.