Compare commits
88 Commits
60d1edcccc
...
v0.1
Author | SHA1 | Date | |
---|---|---|---|
694eb43eab | |||
694d40124f | |||
39778ce08d | |||
cfd44621d5 | |||
4be71317b0 | |||
49642553e1 | |||
1e11b5921d | |||
7343c3b9d9 | |||
9e8410d4cf | |||
bc0c4f33ad | |||
1098fa252f | |||
0b1905429c | |||
e7d3ea3697 | |||
577bde6e57 | |||
60b9ed9eb2 | |||
d13bf883b5 | |||
681e005a68 | |||
2ce89fb39a | |||
907bc26264 | |||
e243e862ae | |||
5e930b9847 | |||
ed3ec885c0 | |||
835bcfe121 | |||
9432496875 | |||
0731e40e6a | |||
35322de3ac | |||
c3d8d6f8e5 | |||
1f2bca5028 | |||
9a9e5cd3e0 | |||
4d828500af | |||
deac5ca5b8 | |||
e05ebeef2a | |||
bdca40bae4 | |||
b4cd46a1e7 | |||
a57acc1176 | |||
ffcf2fb013 | |||
c7a9c8215c | |||
ca4cf2cd68 | |||
e19fc8820a | |||
ee3f2919c6 | |||
653736622f | |||
e3afe52ab7 | |||
905acacd07 | |||
8763fa35dd | |||
feae5d560a | |||
ec268f6047 | |||
6fff5e5cc8 | |||
7b19e553f2 | |||
64ef797727 | |||
85e17ede84 | |||
8e5b39a6e4 | |||
4514d94be9 | |||
4080d1a80a | |||
a36ae22d52 | |||
ad8ac61b98 | |||
e5bb4dfd96 | |||
363188d7d6 | |||
139d6fcb22 | |||
92d9da14c2 | |||
b43fd46260 | |||
a6dc46149c | |||
120038ea8f | |||
8cf09e43c9 | |||
ecc12f6f3b | |||
a1f210fee1 | |||
2662f54c51 | |||
950c25bace | |||
1c0dd7aa0b | |||
5b345e6bf5 | |||
891d8bf7ef | |||
68fc644ea6 | |||
ca9d2aabe4 | |||
7a04ccfd9f | |||
4df9808859 | |||
933418895e | |||
3c56290448 | |||
63f5064ba9 | |||
8e8b6233d6 | |||
89e8674ced | |||
77f40cf3c5 | |||
2008bab1f7 | |||
d244cfbfe1 | |||
6f283c1d12 | |||
aae8402403 | |||
53c303095d | |||
ce25c5fe9f | |||
d95c134a54 | |||
32386ccf58 |
13
.clang-format
Normal file
13
.clang-format
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
AlignConsecutiveShortCaseStatements:
|
||||||
|
Enabled: true
|
||||||
|
AcrossEmptyLines: true
|
||||||
|
AcrossComments: true
|
||||||
|
IndentCaseLabels: true
|
||||||
|
AllowShortBlocksOnASingleLine: Always
|
||||||
|
AllowShortCaseLabelsOnASingleLine: true
|
||||||
|
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
IndentWidth: 4
|
||||||
|
PointerAlignment: Left
|
||||||
|
AlignAfterOpenBracket: BlockIndent
|
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,6 +1,8 @@
|
|||||||
*.o
|
*.o
|
||||||
*.so
|
*.so
|
||||||
|
tags
|
||||||
*.out
|
*.out
|
||||||
obj/
|
|
||||||
compile_commands.json
|
|
||||||
.cache
|
.cache
|
||||||
|
build/*
|
||||||
|
compile_commands.json
|
||||||
|
vgcore.*
|
||||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "test/Unity"]
|
||||||
|
path = test/Unity
|
||||||
|
url = https://github.com/ThrowTheSwitch/Unity
|
104
Makefile
104
Makefile
@@ -1,29 +1,105 @@
|
|||||||
NAME = scl
|
NAME = scl
|
||||||
|
|
||||||
CC = clang
|
TARGET = $(NAME).out
|
||||||
CFLAGS = -Wall --std=c11
|
|
||||||
LDFLAGS =
|
|
||||||
|
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
OBJ_DIR = obj
|
INC_DIR = $(SRC_DIR)/include
|
||||||
TARGET = $(NAME).out
|
BUILD_DIR = build
|
||||||
|
OBJ_DIR = $(BUILD_DIR)/obj
|
||||||
|
GRAM_DIR = $(BUILD_DIR)/grammars
|
||||||
|
TEST_DIR = test
|
||||||
|
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
||||||
|
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
||||||
|
|
||||||
|
CC = clang
|
||||||
|
LINK = clang
|
||||||
|
CFLAGS = -Wall -DDBG -ggdb
|
||||||
|
LDFLAGS = -lm
|
||||||
|
BATS = bats
|
||||||
|
BISON = bison
|
||||||
|
PRINT = echo -e
|
||||||
|
|
||||||
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
||||||
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
|
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
|
||||||
|
OBJ_FILES_NOMAIN = $(filter-out $(OBJ_DIR)/main.o, $(OBJ_FILES)) # Object files without main.c.
|
||||||
|
GRAM_FILES = $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h
|
||||||
|
UNITY_DIR = $(TEST_DIR)/Unity
|
||||||
|
UNITY_C = $(UNITY_DIR)/src/unity.c
|
||||||
|
UNITY_H = $(UNITY_DIR)/src/unity.h
|
||||||
|
UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
|
||||||
|
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
|
||||||
|
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
|
||||||
|
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
|
||||||
|
TEST_VAL_DIR = $(TEST_DIR)/validation
|
||||||
|
|
||||||
|
RESETCOLOR = \033[0m
|
||||||
|
WHITE = $(RESETCOLOR)\033[37m
|
||||||
|
WHITE_BOLD = $(RESETCOLOR)\033[37;1m
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(OBJ_FILES)
|
release: clean
|
||||||
@ echo -e "\x1b[32;1mLinking \x1b[0m\x1b[32m$(TARGET)\x1b[32;1m...\x1b[0m\x1b[37m $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)\x1b[0m"
|
release: CFLAGS = -Wall -O2
|
||||||
@ $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
|
release: $(TARGET)
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/include/%.h
|
# Run the target.
|
||||||
|
run: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
# Generate grammars with bison.
|
||||||
|
$(GRAM_FILES): $(SRC_DIR)/grammar.y
|
||||||
|
@ mkdir -p $(GRAM_DIR)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
|
||||||
|
$(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
|
||||||
|
|
||||||
|
# Compile grammars.
|
||||||
|
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Lexer depends on grammars.
|
||||||
|
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
|
||||||
@ mkdir -p $(OBJ_DIR)
|
@ mkdir -p $(OBJ_DIR)
|
||||||
@ echo -e "\x1b[32;1mCompiling \x1b[0m\x1b[32m$<\x1b[32;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -c $< -o $@\x1b[0m"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||||
@ $(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Compile project source objects.
|
||||||
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
|
||||||
|
@ mkdir -p $(OBJ_DIR)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Link to final binary.
|
||||||
|
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
|
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
|
||||||
|
|
||||||
|
# Compile Unity object.
|
||||||
|
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
|
||||||
|
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
|
||||||
|
|
||||||
|
# Compile test object.
|
||||||
|
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Link final test binary.
|
||||||
|
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
|
$(LINK) -o $@ $? $(LDFLAGS)
|
||||||
|
|
||||||
|
# Run the test files.
|
||||||
|
test: $(TARGET) $(TEST_BIN_FILES)
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
|
||||||
|
for test in $(TEST_BIN_FILES); do ./$${test}; done
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
|
||||||
|
$(BATS) $(TEST_VAL_DIR)
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@ echo -e "\x1b[32;1mCleaning up...\x1b[0m"
|
@ $(PRINT) "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
|
||||||
@ rm -rf $(OBJ_DIR) $(TARGET)
|
rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean test nocolor release run
|
||||||
|
.PRECIOUS: $(TEST_OBJ_FILES)
|
||||||
|
63
README.md
63
README.md
@@ -1 +1,62 @@
|
|||||||
# SCL: Simple Calculator Language
|
# SCL: Simple CAS Language
|
||||||
|
|
||||||
|
Version v1.0-alpha
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.signorovitch.org/jacob/scl && cd scl
|
||||||
|
make release # Build.
|
||||||
|
./scl # Run.
|
||||||
|
```
|
||||||
|
|
||||||
|
If you wish to run tests, make sure to run `git clone --recurse-submodules` to
|
||||||
|
include the [Unity](https://github.com/ThrowTheSwitch/Unity) test framework.
|
||||||
|
|
||||||
|
## Current State
|
||||||
|
|
||||||
|
See [STATUS.md](STATUS.md). Currently, one is able to use `scl` as a basic,
|
||||||
|
interactive, four-function calculator.
|
||||||
|
|
||||||
|
## Syntax (Planned)
|
||||||
|
|
||||||
|
As one would expect, you can evaluate simple infix expressions:
|
||||||
|
|
||||||
|
```scl
|
||||||
|
> 1 + 1
|
||||||
|
= 2
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also define your own functions:
|
||||||
|
|
||||||
|
```scl
|
||||||
|
> f(x) = 2x
|
||||||
|
> f(2)
|
||||||
|
= 4
|
||||||
|
```
|
||||||
|
|
||||||
|
Symbolic algebra is done in the following manner:
|
||||||
|
|
||||||
|
```scl
|
||||||
|
> f(x) = x^4
|
||||||
|
> diff(f, x:sym, 2)
|
||||||
|
= 12x^2
|
||||||
|
```
|
||||||
|
|
||||||
|
SCL will dynamically decide on types, but you can state them explicitly as
|
||||||
|
well:
|
||||||
|
|
||||||
|
```scl
|
||||||
|
> f(x:int) = 2x
|
||||||
|
> f(2.2)
|
||||||
|
! f(x:int): x must be of type int.
|
||||||
|
```
|
||||||
|
|
||||||
|
Variables can be defined, with several attributes:
|
||||||
|
|
||||||
|
```scl
|
||||||
|
> a = 1 // Interpret type automatically.
|
||||||
|
> b:int = 1 // Must be int.
|
||||||
|
> c:const:int = 1 // Constant: value can never change.
|
||||||
|
> x:sym // Treated symbolicaly.
|
||||||
|
```
|
||||||
|
55
STATUS.md
Normal file
55
STATUS.md
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# SCL Design Status
|
||||||
|
|
||||||
|
- [x] Data definitions
|
||||||
|
- [x] Token Definitions
|
||||||
|
- [x] AST Definitions
|
||||||
|
|
||||||
|
- [ ] Parser
|
||||||
|
- [x] Parse numbers
|
||||||
|
- [x] Parse floats
|
||||||
|
- [x] Parse negative numbers
|
||||||
|
- [x] Parse infix operators
|
||||||
|
- [x] Order of operations
|
||||||
|
- [x] Parse function application
|
||||||
|
- [x] Parse order of operations with parenthesis
|
||||||
|
- [ ] Parse variable invocation
|
||||||
|
- [ ] Parse variable definition
|
||||||
|
- [ ] Parse types
|
||||||
|
- [ ] Parse function definition
|
||||||
|
- [ ] Parse lists/arrays/vectors
|
||||||
|
- [ ] Parse blocks
|
||||||
|
- [ ] Parse control flow
|
||||||
|
- [ ] Parse `if` statements
|
||||||
|
- [ ] Parse `loop`s
|
||||||
|
- [ ] Parse `for` loops
|
||||||
|
- [ ] Parse `while` loops
|
||||||
|
- [ ] Parse `case` statements
|
||||||
|
- [ ] Parse `goto` statements
|
||||||
|
- [ ] Parse lambda function definition
|
||||||
|
- [ ] Parse function calling with positional arguments
|
||||||
|
- [ ] Parse variadic functions
|
||||||
|
- [ ] Parse infix function definition
|
||||||
|
|
||||||
|
- [ ] Executer
|
||||||
|
- [x] Exec function calls
|
||||||
|
- [ ] Exec variable use
|
||||||
|
- [ ] Exec variable definition
|
||||||
|
- [ ] Exec function definition
|
||||||
|
- [ ] Exec symbolic variables
|
||||||
|
- [ ] Exec control flow statements
|
||||||
|
- [ ] Exec variadic functions
|
||||||
|
- [ ] Exec lambda functions
|
||||||
|
- [ ] Exec lists
|
||||||
|
- [ ] Exec arrays
|
||||||
|
- [ ] Exec vectors
|
||||||
|
|
||||||
|
- [ ] Interface
|
||||||
|
- [ ] Interactive interpreter
|
||||||
|
- [ ] Use GNU readline
|
||||||
|
- [ ] Multi-line input
|
||||||
|
- [ ] Syntax highlighting
|
||||||
|
- [ ] Autocompletion/suggestion
|
||||||
|
- [ ] Command line interface
|
||||||
|
- [ ] Pass in a file
|
||||||
|
- [ ] Save AST to a file
|
||||||
|
- [ ] Run from AST file
|
1
examples/test.scl
Normal file
1
examples/test.scl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
1 + 1
|
117
src/ast.c
Normal file
117
src/ast.c
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/dstr.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
extern AST* root;
|
||||||
|
|
||||||
|
static char* asttype_names[] = {
|
||||||
|
[AST_TYPE_CALL] = "FUNC CALL",
|
||||||
|
[AST_TYPE_NUM] = "NUMBER",
|
||||||
|
[AST_TYPE_VREF] = "VAR REFERENCE"
|
||||||
|
};
|
||||||
|
|
||||||
|
AST* ast_init(ASTType type, void* data) {
|
||||||
|
AST* ast = malloc(sizeof(AST));
|
||||||
|
|
||||||
|
ast->type = type;
|
||||||
|
ast->data = data;
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_destroy(AST* ast) {
|
||||||
|
if (!ast) return;
|
||||||
|
|
||||||
|
switch (ast->type) {
|
||||||
|
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
|
||||||
|
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
|
||||||
|
default:
|
||||||
|
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
||||||
|
|
||||||
|
void ast_print_i(AST* ast, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("AST", ast);
|
||||||
|
INDENT_FIELD("type", "%s", asttype_names[ast->type]);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("data");
|
||||||
|
switch (ast->type) {
|
||||||
|
case AST_TYPE_NUM:
|
||||||
|
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
||||||
|
break;
|
||||||
|
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
||||||
|
default: exit(1);
|
||||||
|
}
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNumData* ast_num_data_init(double val) {
|
||||||
|
talloc(ASTNumData, num);
|
||||||
|
|
||||||
|
*num = val;
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_num_data_destroy(ASTNumData* num) { free(num); }
|
||||||
|
|
||||||
|
void ast_num_print(ASTNumData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_FIELD("data", "%lf", *data);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
||||||
|
talloc(ASTCallData, call);
|
||||||
|
|
||||||
|
log_dbgf("to: %s", to);
|
||||||
|
|
||||||
|
call->to = to;
|
||||||
|
call->argc = argc;
|
||||||
|
call->argv = argv;
|
||||||
|
|
||||||
|
return call;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_call_data_destroy(ASTCallData* call) {
|
||||||
|
if (!call) return;
|
||||||
|
free(call->to);
|
||||||
|
for (size_t i = 0; i < call->argc; i++) ast_destroy(call->argv[i]);
|
||||||
|
free(call->argv);
|
||||||
|
free(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_call_print(ASTCallData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTCallData", data);
|
||||||
|
INDENT_FIELD("to", "%s", data->to);
|
||||||
|
INDENT_FIELD("argc", "%ld", data->argc);
|
||||||
|
INDENT_FIELD_LIST("argv", data->argv, data->argc, ast_print_i);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTVrefData* ast_vref_data_init(char* to) {}
|
||||||
|
|
||||||
|
void ast_vref_data_destroy(ASTVrefData* vref) {}
|
||||||
|
|
||||||
|
void ast_vref_print(ASTVrefData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTVrefData", data);
|
||||||
|
INDENT_FIELD("to", "%s", data->to);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
54
src/dstr.c
Normal file
54
src/dstr.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include "include/dstr.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
Dstr* dstr_init(void) {
|
||||||
|
Dstr* dstr = malloc(sizeof(Dstr));
|
||||||
|
|
||||||
|
dstr->sz = DSTR_INITSZ;
|
||||||
|
dstr->buf = malloc(DSTR_INITSZ);
|
||||||
|
*dstr->buf = '\0';
|
||||||
|
dstr->ln = 0;
|
||||||
|
|
||||||
|
return dstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dstr_destroy(Dstr* dstr) {
|
||||||
|
free(dstr->buf);
|
||||||
|
free(dstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dstr_destroypsv(Dstr* dstr) { free(dstr); }
|
||||||
|
|
||||||
|
// Check whether the buffer is overflowing and resize it if necessary.
|
||||||
|
void check_resz(Dstr* dstr, size_t ln) {
|
||||||
|
while (dstr->ln + ln + 1 > dstr->sz) {
|
||||||
|
// Double the buffer size when overflown.
|
||||||
|
dstr->sz *= 2;
|
||||||
|
dstr->buf = realloc(dstr->buf, dstr->sz);
|
||||||
|
log_dbgf(
|
||||||
|
"dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2, dstr->sz
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dstr_append(Dstr* dest, char* src, size_t ln) {
|
||||||
|
check_resz(dest, ln);
|
||||||
|
|
||||||
|
// Overwrites the \0 at the end of the string, keeps the null from the given
|
||||||
|
// string.
|
||||||
|
memcpy(dest->buf + dest->ln, src, ln + 1);
|
||||||
|
dest->ln += ln;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dstr_appendch(Dstr* dest, char ch) {
|
||||||
|
check_resz(dest, 1);
|
||||||
|
|
||||||
|
// Overwrites the preexisting null terminator, and adds one of its own.
|
||||||
|
dest->buf[dest->ln] = ch;
|
||||||
|
dest->buf[dest->ln + 1] = '\0';
|
||||||
|
dest->ln += 1;
|
||||||
|
}
|
70
src/exec.c
Normal file
70
src/exec.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/exec.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
extern AST* root;
|
||||||
|
|
||||||
|
ASTNumData exec_exp(AST* ast) {
|
||||||
|
log_dbg("Started execution.");
|
||||||
|
switch (ast->type) {
|
||||||
|
case AST_TYPE_CALL: return exec_call(ast);
|
||||||
|
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
|
||||||
|
default: printf("what\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNumData exec_call(AST* ast) {
|
||||||
|
log_dbg("Started call execution.");
|
||||||
|
fflush(stdout);
|
||||||
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
|
if (calldata->argc >= 1) {
|
||||||
|
if (!strcmp(calldata->to, "sum")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total += exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
} else if (!strcmp(calldata->to, "sub")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total -= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
} else if (!strcmp(calldata->to, "mul")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total *= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
} else if (!strcmp(calldata->to, "div")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total /= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}}
|
||||||
|
return -1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_print(double n) { printf("= %lf\n", n); }
|
131
src/grammar.y
Normal file
131
src/grammar.y
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
%{
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../../src/include/ast.h"
|
||||||
|
#include "../../src/include/lexer.h"
|
||||||
|
|
||||||
|
int yylex(void);
|
||||||
|
void yyerror(char const*);
|
||||||
|
|
||||||
|
AST* root = NULL;
|
||||||
|
%}
|
||||||
|
|
||||||
|
%code requires {
|
||||||
|
#include "../../src/include/ast.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
%union {
|
||||||
|
double fval;
|
||||||
|
char* strval;
|
||||||
|
AST* ast;
|
||||||
|
ArgArr* argarr;
|
||||||
|
}
|
||||||
|
|
||||||
|
%define parse.error verbose
|
||||||
|
|
||||||
|
%token LGROUP
|
||||||
|
%token RGROUP
|
||||||
|
%token SEP
|
||||||
|
|
||||||
|
%token<strval> WORD
|
||||||
|
%token<fval> NUM
|
||||||
|
|
||||||
|
%token SUB
|
||||||
|
%token PLUS
|
||||||
|
%token MULT
|
||||||
|
%token DIV
|
||||||
|
|
||||||
|
%token NL
|
||||||
|
|
||||||
|
%left PLUS SUB
|
||||||
|
%left MULT DIV
|
||||||
|
%precedence NEG
|
||||||
|
|
||||||
|
%type<ast> exp;
|
||||||
|
%type<argarr> arg;
|
||||||
|
%type<argarr> argstart;
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
input:
|
||||||
|
%empty
|
||||||
|
| exp { root = $1; }
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
argstart:
|
||||||
|
exp {
|
||||||
|
ArgArr* argarr = argarr_init();
|
||||||
|
argarr_add(argarr, $1);
|
||||||
|
$$ = argarr;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
arg:
|
||||||
|
argstart { $$ = $1; }
|
||||||
|
| arg SEP exp {
|
||||||
|
argarr_add($1, $3);
|
||||||
|
$$ = $1;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
exp:
|
||||||
|
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
||||||
|
|
||||||
|
| SUB exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
|
||||||
|
argv[1] = $2;
|
||||||
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "mul");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| LGROUP exp RGROUP { $$ = $2; }
|
||||||
|
|
||||||
|
// Variable reference.
|
||||||
|
//| WORD
|
||||||
|
|
||||||
|
| WORD LGROUP arg RGROUP {
|
||||||
|
size_t argc = $3->ln;
|
||||||
|
AST** argv = $3->buf;
|
||||||
|
argarr_destroypsv($3);
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| exp PLUS exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = $1;
|
||||||
|
argv[1] = $3;
|
||||||
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "sum");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| exp SUB exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = $1;
|
||||||
|
argv[1] = $3;
|
||||||
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "sub");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| exp MULT exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = $1;
|
||||||
|
argv[1] = $3;
|
||||||
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "mul");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| exp DIV exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = $1;
|
||||||
|
argv[1] = $3;
|
||||||
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "div");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
%%
|
47
src/include/ast.h
Normal file
47
src/include/ast.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef AST_H
|
||||||
|
#define AST_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
AST_TYPE_NUM, // A number.
|
||||||
|
AST_TYPE_CALL, // A function call.
|
||||||
|
AST_TYPE_VREF, // A variable reference.
|
||||||
|
AST_TYPE_MAX = AST_TYPE_CALL
|
||||||
|
} ASTType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ASTType type;
|
||||||
|
void* data;
|
||||||
|
} AST;
|
||||||
|
|
||||||
|
AST* ast_init(ASTType type, void* data);
|
||||||
|
void ast_destroy(AST* ast);
|
||||||
|
void ast_print(AST* ast);
|
||||||
|
void ast_print_i(AST* ast, int i);
|
||||||
|
|
||||||
|
typedef double ASTNumData;
|
||||||
|
|
||||||
|
ASTNumData* ast_num_data_init(double val);
|
||||||
|
void ast_num_data_destroy(ASTNumData* num);
|
||||||
|
void ast_num_print(ASTNumData*, int i);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* to; // What the call's to.
|
||||||
|
size_t argc; // Argument count.
|
||||||
|
AST** argv; // Argument vector.
|
||||||
|
} ASTCallData;
|
||||||
|
|
||||||
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
||||||
|
void ast_call_data_destroy(ASTCallData* call);
|
||||||
|
void ast_call_print(ASTCallData*, int i);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* to; // What the reference's to.
|
||||||
|
} ASTVrefData;
|
||||||
|
|
||||||
|
ASTVrefData* ast_vref_data_init(char* to);
|
||||||
|
void ast_vref_data_destroy(ASTVrefData* call);
|
||||||
|
void ast_vref_print(ASTVrefData*, int i);
|
||||||
|
|
||||||
|
#endif
|
25
src/include/dstr.h
Normal file
25
src/include/dstr.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef DSTR_H
|
||||||
|
#define DSTR_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define DSTR_INITSZ 2
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* buf; // The buffer containing the string.
|
||||||
|
size_t sz; // The size of the buffer.
|
||||||
|
size_t ln; // The number of characters in the buffer.
|
||||||
|
} Dstr;
|
||||||
|
|
||||||
|
Dstr* dstr_init(void);
|
||||||
|
void dstr_destroy(Dstr* dstr);
|
||||||
|
// Destroy Dstr structure but preserve ->buf.
|
||||||
|
void dstr_destroypsv(Dstr* dstr);
|
||||||
|
|
||||||
|
// Append ln characters of src to dest.
|
||||||
|
void dstr_append(Dstr* dest, char* src, size_t ln);
|
||||||
|
|
||||||
|
// Append ch to dest.
|
||||||
|
void dstr_appendch(Dstr* dest, char ch);
|
||||||
|
|
||||||
|
#endif
|
10
src/include/exec.h
Normal file
10
src/include/exec.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef EXEC_H
|
||||||
|
#define EXEC_H
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
ASTNumData exec_exp(AST* ast);
|
||||||
|
ASTNumData exec_call(AST* ast);
|
||||||
|
void exec_print(double n);
|
||||||
|
|
||||||
|
#endif
|
39
src/include/lexer.h
Normal file
39
src/include/lexer.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef LEXER_H
|
||||||
|
#define LEXER_H
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
#define ARLN 8
|
||||||
|
|
||||||
|
extern char* inp;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t sz;
|
||||||
|
size_t ln;
|
||||||
|
AST** buf;
|
||||||
|
} ArgArr;
|
||||||
|
|
||||||
|
ArgArr* argarr_init();
|
||||||
|
void argarr_destroy(ArgArr* argarr);
|
||||||
|
// Destroy ArgArr structure but preserve -> buf.
|
||||||
|
void argarr_destroypsv(ArgArr* argarr);
|
||||||
|
void argarr_add(ArgArr* argarr, AST* arg);
|
||||||
|
|
||||||
|
#include "../../build/grammars/grammar.tab.h"
|
||||||
|
|
||||||
|
extern YYSTYPE yylval;
|
||||||
|
|
||||||
|
// Accumulate an integer.
|
||||||
|
int acc_int(int c);
|
||||||
|
|
||||||
|
// Accumulate a floating-point number.
|
||||||
|
double acc_float(int c);
|
||||||
|
|
||||||
|
// Called by `yyparse()` (in bison-generated files.)
|
||||||
|
int yylex();
|
||||||
|
void yyerror(char const* s);
|
||||||
|
|
||||||
|
#endif
|
@@ -1,6 +1,2 @@
|
|||||||
#ifndef MAIN_H
|
// This file serves no purpose but because I've written my makefile this way it
|
||||||
#define MAIN_H
|
// has to exist for things to compile :P. TODO: Fix this.
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
@@ -1,30 +0,0 @@
|
|||||||
#ifndef PARSER_H
|
|
||||||
#define PARSER_H
|
|
||||||
|
|
||||||
// Expression one of:
|
|
||||||
// - Operation
|
|
||||||
// - Number
|
|
||||||
|
|
||||||
// Operation contains:
|
|
||||||
// - Type
|
|
||||||
// - Expression 1
|
|
||||||
// - Expression 2
|
|
||||||
|
|
||||||
typedef enum OpType {
|
|
||||||
OPTYPE_PLUS,
|
|
||||||
OPTYPE_MINUS
|
|
||||||
} optype_t;
|
|
||||||
|
|
||||||
typedef union Exp {
|
|
||||||
typedef struct Op {
|
|
||||||
optype_t type;
|
|
||||||
Exp* exp1;
|
|
||||||
Exp* exp2;
|
|
||||||
} op_t;
|
|
||||||
|
|
||||||
int n;
|
|
||||||
} exp_t;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
23
src/include/stack.h
Normal file
23
src/include/stack.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef STACK_H
|
||||||
|
#define STACK_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define STACK_MAX 64
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t i; // Current index in the stack.
|
||||||
|
void* val[STACK_MAX]; // The stack itself.
|
||||||
|
} Stack;
|
||||||
|
|
||||||
|
Stack* stack_init();
|
||||||
|
// Destroy a stack.
|
||||||
|
// Note that `stack->i` must be `0`.
|
||||||
|
void stack_destroy(Stack* stack);
|
||||||
|
|
||||||
|
// Push a value to the stack.
|
||||||
|
void stack_push(Stack* stack, void* val);
|
||||||
|
// Pop a value from the stack.
|
||||||
|
void* stack_pop(Stack* stack);
|
||||||
|
|
||||||
|
#endif
|
@@ -1,12 +0,0 @@
|
|||||||
#ifndef UTIL_H
|
|
||||||
#define UTIL_H
|
|
||||||
|
|
||||||
// Utilies.
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
// Exit with an error. Returns int for ease of use, but should be treated as void.
|
|
||||||
int die(char* msg);
|
|
||||||
|
|
||||||
#endif
|
|
85
src/include/util.h
Normal file
85
src/include/util.h
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#ifndef UTIL_H
|
||||||
|
#define UTIL_H
|
||||||
|
|
||||||
|
#ifdef DBG // Debug macros
|
||||||
|
|
||||||
|
// Log a message.
|
||||||
|
#define log_dbg(msg) \
|
||||||
|
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
|
||||||
|
__func__);
|
||||||
|
|
||||||
|
// Log a message with formatting.
|
||||||
|
#define log_dbgf(msg, ...) \
|
||||||
|
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
|
||||||
|
__func__, __VA_ARGS__);
|
||||||
|
|
||||||
|
#else // ifdef DBG
|
||||||
|
#define log_dbg(msg)
|
||||||
|
#define log_dbgf(msg, ...)
|
||||||
|
#endif // ifdef DBG else
|
||||||
|
|
||||||
|
// Resent color code.
|
||||||
|
#define COL_RESET "\e[0m"
|
||||||
|
|
||||||
|
// Regular color codes.
|
||||||
|
#define COL_BLA "\e[0;30m"
|
||||||
|
#define COL_RED "\e[0;31m"
|
||||||
|
#define COL_GRE "\e[0;32m"
|
||||||
|
#define COL_YEL "\e[0;33m"
|
||||||
|
#define COL_BLU "\e[0;34m"
|
||||||
|
#define COL_MAG "\e[0;35m"
|
||||||
|
#define COL_CYA "\e[0;36m"
|
||||||
|
#define COL_WHI "\e[0;37m"
|
||||||
|
|
||||||
|
// Bold color codes.
|
||||||
|
#define COL_BBLA "\e[1;30m"
|
||||||
|
#define COL_BRED "\e[1;31m"
|
||||||
|
#define COL_BGRE "\e[1;32m"
|
||||||
|
#define COL_BYEL "\e[1;33m"
|
||||||
|
#define COL_BBLU "\e[1;34m"
|
||||||
|
#define COL_BMAG "\e[1;35m"
|
||||||
|
#define COL_BCYA "\e[1;36m"
|
||||||
|
#define COL_BWHI "\e[1;37m"
|
||||||
|
|
||||||
|
// Start in indent block.
|
||||||
|
#define INDENT_BEGIN(ILVL) \
|
||||||
|
__attribute__((unused)) int INDENT_lvl = ILVL; \
|
||||||
|
Dstr* INDENT_spacing = dstr_init(); \
|
||||||
|
for (int INDENT_j = 0; INDENT_j < ILVL; INDENT_j++) \
|
||||||
|
dstr_appendch(INDENT_spacing, ' ');
|
||||||
|
|
||||||
|
// Print & indent the title of a section.
|
||||||
|
#define INDENT_TITLE(THING, WHERE) \
|
||||||
|
printf("%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, INDENT_spacing->buf, WHERE);
|
||||||
|
|
||||||
|
// Print & indent a thing.
|
||||||
|
#define INDENT_FIELD(FIELD, VAL, ...) \
|
||||||
|
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \
|
||||||
|
INDENT_spacing->buf, __VA_ARGS__);
|
||||||
|
|
||||||
|
// Print & indent a thing with a newline before the val.
|
||||||
|
#define INDENT_FIELD_NL(FIELD, VAL, ...) \
|
||||||
|
printf("%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
|
||||||
|
"\n", \
|
||||||
|
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
|
||||||
|
|
||||||
|
// Print & indent a thing without any newline.
|
||||||
|
#define INDENT_FIELD_EXT_NONL_START(FIELD) \
|
||||||
|
printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf);
|
||||||
|
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
|
||||||
|
|
||||||
|
// Print an array A of N things, by calling the function F.
|
||||||
|
#define INDENT_FIELD_LIST(FIELD, A, N, F) \
|
||||||
|
printf("%s " COL_BWHI FIELD ": [\n" COL_RESET, INDENT_spacing->buf); \
|
||||||
|
for (int INDENT_i = 0; INDENT_i < N; INDENT_i++) { \
|
||||||
|
F(A[INDENT_i], INDENT_lvl + 2); \
|
||||||
|
} \
|
||||||
|
printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
|
||||||
|
|
||||||
|
// End an indent block.
|
||||||
|
#define INDENT_END printf(COL_RESET); dstr_destroy(INDENT_spacing);
|
||||||
|
|
||||||
|
// Allocate a pointer with a type.
|
||||||
|
#define talloc(T, X) T* X = malloc(sizeof(T));
|
||||||
|
|
||||||
|
#endif
|
120
src/lexer.c
Normal file
120
src/lexer.c
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
#include <complex.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/dstr.h"
|
||||||
|
#include "include/lexer.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
ArgArr* argarr_init() {
|
||||||
|
ArgArr* argarr = malloc(sizeof(ArgArr));
|
||||||
|
|
||||||
|
argarr->sz = ARLN * sizeof(AST*);
|
||||||
|
argarr->ln = 0;
|
||||||
|
argarr->buf = malloc(argarr->sz);
|
||||||
|
|
||||||
|
return argarr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void argarr_destroy(ArgArr* argarr) {
|
||||||
|
free(argarr->buf);
|
||||||
|
free(argarr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
|
||||||
|
|
||||||
|
void argarr_add(ArgArr* argarr, AST* arg) {
|
||||||
|
if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
|
||||||
|
argarr->sz *= 2;
|
||||||
|
argarr->buf = realloc(argarr->buf, argarr->sz);
|
||||||
|
log_dbgf(
|
||||||
|
"ArgArr @ %p doubled from %ld to %ld", argarr, argarr->sz / 2,
|
||||||
|
argarr->sz
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
argarr->buf[argarr->ln++] = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
int acc_int(int c) {
|
||||||
|
int value = c - '0';
|
||||||
|
while (isdigit(*inp)) {
|
||||||
|
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||||
|
inp++;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double acc_float(int c) {
|
||||||
|
int dplaces = 0;
|
||||||
|
double value = (double)(c - '0');
|
||||||
|
|
||||||
|
// Grab everything prior to '.'.
|
||||||
|
while (isdigit(*inp)) {
|
||||||
|
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||||
|
inp++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*inp == '.') {
|
||||||
|
inp++;
|
||||||
|
|
||||||
|
while (isdigit(*inp)) {
|
||||||
|
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||||
|
dplaces++;
|
||||||
|
inp++;
|
||||||
|
}
|
||||||
|
value = value / pow(10, dplaces);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* acc_word(int c) {
|
||||||
|
Dstr* val = dstr_init();
|
||||||
|
do {
|
||||||
|
dstr_appendch(val, *(inp - 1));
|
||||||
|
inp++;
|
||||||
|
} while (isalpha(*inp));
|
||||||
|
dstr_appendch(val, *(inp - 1));
|
||||||
|
|
||||||
|
char* ret = val->buf;
|
||||||
|
dstr_destroypsv(val);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int yylex() {
|
||||||
|
if (*inp == '\0') return YYEOF;
|
||||||
|
|
||||||
|
// Skip all whitespace.
|
||||||
|
while (*inp == ' ' || *inp == '\t') inp++;
|
||||||
|
|
||||||
|
// Assign & consume current character.
|
||||||
|
int c = *inp++;
|
||||||
|
|
||||||
|
// Check for NUM.
|
||||||
|
if (isdigit(c)) {
|
||||||
|
yylval.fval = acc_float(c); // Set the token value.
|
||||||
|
return NUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isalpha(c)) {
|
||||||
|
yylval.strval = acc_word(c);
|
||||||
|
return WORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case '+': return PLUS;
|
||||||
|
case '\n': return NL;
|
||||||
|
case '-': return SUB;
|
||||||
|
case '*': return MULT;
|
||||||
|
case '/': return DIV;
|
||||||
|
case '(': return LGROUP;
|
||||||
|
case ')': return RGROUP;
|
||||||
|
case ',': return SEP;
|
||||||
|
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void yyerror(char const* s) { fprintf(stderr, "Parse error: %s\n", s); }
|
64
src/main.c
64
src/main.c
@@ -1,6 +1,68 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/dstr.h"
|
||||||
|
#include "include/exec.h"
|
||||||
|
#include "include/lexer.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
#include "../build/grammars/grammar.tab.h"
|
||||||
|
|
||||||
|
// Global Abstract Syntax Tree.
|
||||||
|
extern AST* root;
|
||||||
|
|
||||||
|
// Global input text.
|
||||||
|
char* inp = NULL;
|
||||||
|
|
||||||
|
extern int yyparse();
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
printf("Hello, world!\n");
|
|
||||||
|
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
||||||
|
log_dbg("Parsed successfully!\n");
|
||||||
|
exec_print(exec_exp(root));
|
||||||
|
ast_destroy(root);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
Dstr* ln = dstr_init();
|
||||||
|
char c;
|
||||||
|
|
||||||
|
printf("> ");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
// Accumulate line.
|
||||||
|
do {
|
||||||
|
c = getc(stdin);
|
||||||
|
switch (c) {
|
||||||
|
case EOF: dstr_destroy(ln); goto lnskip;
|
||||||
|
case '\n': goto lnend;
|
||||||
|
default: dstr_appendch(ln, c); log_dbgf("cchar: %c", c);
|
||||||
|
}
|
||||||
|
} while (1);
|
||||||
|
|
||||||
|
lnend:
|
||||||
|
|
||||||
|
log_dbgf("cline: %s", ln->buf);
|
||||||
|
|
||||||
|
if (ln->ln > 0) {
|
||||||
|
inp = ln->buf;
|
||||||
|
if (yyparse() == 0) {
|
||||||
|
log_dbg("Parsed successfully!\n");
|
||||||
|
} else printf("Parse error.\n");
|
||||||
|
|
||||||
|
exec_print(exec_exp(root));
|
||||||
|
#ifdef DBG
|
||||||
|
ast_print(root);
|
||||||
|
#endif
|
||||||
|
ast_destroy(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
dstr_destroy(ln);
|
||||||
|
}
|
||||||
|
lnskip:;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
40
src/stack.c
Normal file
40
src/stack.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "include/stack.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
Stack* stack_init() {
|
||||||
|
talloc(Stack, stack);
|
||||||
|
|
||||||
|
memset(stack->val, 0, sizeof(void*) * STACK_MAX);
|
||||||
|
stack->i = 0;
|
||||||
|
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_destroy(Stack* stack) {
|
||||||
|
// Can only free an empty stack.
|
||||||
|
assert(stack->i == 0);
|
||||||
|
free(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_push(Stack* stack, void* val) {
|
||||||
|
if (stack->i >= STACK_MAX) {
|
||||||
|
log_dbgf("Ran out of stack (max: %d)", STACK_MAX);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stack->val[stack->i] = val;
|
||||||
|
stack->i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* stack_pop(Stack* stack) {
|
||||||
|
if (stack->i <= 0) {
|
||||||
|
log_dbg("Can't pop empty stack.");
|
||||||
|
return (void*)-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stack->val[--stack->i];
|
||||||
|
}
|
1
src/util.c
Normal file
1
src/util.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "include/util.h"
|
1
test/Unity
Submodule
1
test/Unity
Submodule
Submodule test/Unity added at 73237c5d22
79
test/test_dstr.c
Normal file
79
test/test_dstr.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include "../src/include/dstr.h"
|
||||||
|
#include "Unity/src/unity.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void setUp() {};
|
||||||
|
void tearDown() {};
|
||||||
|
|
||||||
|
void test_dstr_init() {
|
||||||
|
Dstr* dstr = dstr_init();
|
||||||
|
TEST_ASSERT_EQUAL(0, strlen(dstr->buf));
|
||||||
|
TEST_ASSERT_EQUAL(0, dstr->ln);
|
||||||
|
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_dstr_append() {
|
||||||
|
Dstr* dstr;
|
||||||
|
|
||||||
|
// Test simple appending.
|
||||||
|
dstr = dstr_init();
|
||||||
|
char* hello_world = "Hello, world!";
|
||||||
|
dstr_append(dstr, hello_world, strlen(hello_world));
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_STRING(hello_world, dstr->buf);
|
||||||
|
TEST_ASSERT_EQUAL(strlen(hello_world), dstr->ln);
|
||||||
|
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||||
|
|
||||||
|
dstr_destroy(dstr);
|
||||||
|
|
||||||
|
// Test buffer doubling.
|
||||||
|
dstr = dstr_init();
|
||||||
|
|
||||||
|
char h[DSTR_INITSZ + 20];
|
||||||
|
memset(h, 'h', DSTR_INITSZ + 19);
|
||||||
|
h[DSTR_INITSZ + 19] = '\0';
|
||||||
|
dstr_append(dstr, h, strlen(h));
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
|
||||||
|
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
|
||||||
|
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_dstr_appendch() {
|
||||||
|
Dstr* dstr;
|
||||||
|
|
||||||
|
// Test simple character appending.
|
||||||
|
dstr = dstr_init();
|
||||||
|
char c = 'c';
|
||||||
|
char* c_str = "c";
|
||||||
|
dstr_appendch(dstr, c);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_STRING(c_str, dstr->buf);
|
||||||
|
TEST_ASSERT_EQUAL(strlen(c_str), dstr->ln);
|
||||||
|
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||||
|
|
||||||
|
dstr_destroy(dstr);
|
||||||
|
|
||||||
|
// Test buffer doubling.
|
||||||
|
dstr = dstr_init();
|
||||||
|
|
||||||
|
// Test against this string.
|
||||||
|
char h[DSTR_INITSZ + 20];
|
||||||
|
memset(h, 'h', DSTR_INITSZ + 19);
|
||||||
|
h[DSTR_INITSZ + 19] = '\0';
|
||||||
|
|
||||||
|
for (int i = 0; i < DSTR_INITSZ + 19; i++) dstr_appendch(dstr, 'h');
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
|
||||||
|
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
|
||||||
|
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
// not needed when using generate_test_runner.rb
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_dstr_init);
|
||||||
|
RUN_TEST(test_dstr_append);
|
||||||
|
RUN_TEST(test_dstr_appendch);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
84
test/validation/test.bats
Normal file
84
test/validation/test.bats
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
bin() { ./scl.out $1 | tail -n1; }
|
||||||
|
|
||||||
|
@test "simple addition" {
|
||||||
|
run bin "1+1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1+1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "1+-1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "-1+-1"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple subtraction" {
|
||||||
|
run bin "1-1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "-1-1"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
|
||||||
|
run bin "1--1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1--1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple multiplication" {
|
||||||
|
run bin "1*2"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1*2"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
|
||||||
|
run bin "1*-1"
|
||||||
|
[ "$output" = "= -1.000000" ]
|
||||||
|
|
||||||
|
run bin "-1*-1"
|
||||||
|
[ "$output" = "= 1.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple division" {
|
||||||
|
run bin "1/2"
|
||||||
|
[ "$output" = "= 0.500000" ]
|
||||||
|
|
||||||
|
run bin "-1/2"
|
||||||
|
[ "$output" = "= -0.500000" ]
|
||||||
|
|
||||||
|
run bin "1/-1"
|
||||||
|
[ "$output" = "= -1.000000" ]
|
||||||
|
|
||||||
|
run bin "-1/-1"
|
||||||
|
[ "$output" = "= 1.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "order of operations" {
|
||||||
|
run bin "1+2*3"
|
||||||
|
[ "$output" = "= 7.000000" ]
|
||||||
|
|
||||||
|
run bin "2*3+1"
|
||||||
|
[ "$output" = "= 7.000000" ]
|
||||||
|
|
||||||
|
run bin "6/2-1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "1-6/2"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "order of operations with parenthesis" {
|
||||||
|
run bin "(1+2)*3"
|
||||||
|
[ "$output" = "= 9.000000" ]
|
||||||
|
|
||||||
|
run bin "-(1+2*3)"
|
||||||
|
[ "$output" = "= -7.000000" ]
|
||||||
|
|
||||||
|
run bin "-(-(1+2)*3)"
|
||||||
|
[ "$output" = "= 9.000000" ]
|
||||||
|
}
|
Reference in New Issue
Block a user