Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .github/workflows/build-from-source-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ jobs:
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DBUILD_TESTS=ON
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}/src
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build-release-binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ jobs:
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DPORTABLE=ON
-DBUILD_TESTS=ON
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}/src
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Cpp build files
build/
build/

# Clangd
.cache/
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"name": "Mac",
"includePath": [
"${workspaceFolder}/src",
"${workspaceFolder}/build"
"${workspaceFolder}/build",
"${workspaceFolder}/build/src"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/math-lang",
"program": "${workspaceFolder}/build/src/math-lang",
"args": ["${workspaceFolder}/examples/cosine-law.mls"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.10)

project(MathLang VERSION 0.1.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(DEBUG "Build with debug information" OFF)
option(PORTABLE "Build portable version" OFF)
option(BUILD_TESTS "Build tests" ON)

include_directories(${PROJECT_BINARY_DIR}/src)
add_subdirectory(src)

if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ Toy scripting language to calculate math

### Build binary from source

make sure you have installed `git`, `cmake`, any C++ build system (`make` by default), and any C++ compiler (`g++`, `clang++`, `MVSC`)
make sure you have installed `git`, `cmake`, any C++ build system (`make` by default), and any C++ compiler (`g++`, `clang++`, `MSVC`)
```bash
git clone https://github.com/revival0728/math-lang.git
cd math-lang && cmake -B build -S src && cmake --build build --config Release
cd math-lang
cmake -B build -S . && cmake --build build --config Release -DBUILD_TESTS=OFF
```

### Download from Release
Expand All @@ -21,7 +22,7 @@ You can download `math-lang` binary from [Release](https://github.com/revival072
2. run the binary

```bash
./math-lang/build/math-lang
./build/src/math-lang
```

## Binary Usage
Expand All @@ -45,7 +46,7 @@ fun-call = [fun-name]([[expr] | [expr], ...])
```
The scripts execute based on `expr`.

You can checkout MLS scripts exmaples in the [`examples/`](/examples/) folder.
You can checkout MLS script exmaples in the [`examples/`](/examples/) folder.

## Builtins

Expand All @@ -56,7 +57,7 @@ You can checkout MLS scripts exmaples in the [`examples/`](/examples/) folder.
| pi | 3.14159265358979323846264338327950288 |
| e | 2.71828182845904523536028747135266250 |

## Functions
### Functions

| name | document |
|------|----------|
Expand All @@ -80,4 +81,7 @@ You can checkout MLS scripts exmaples in the [`examples/`](/examples/) folder.
| ln(x) | `CPP` `<cmath>` function (`log` which computes natural logarithm) |
| mod(a, b) | `CPP` `<cmath>` function (`fmod` which computes remainder of division) |

You can checkout [`mathlib.hpp`](/src/mathlib.hpp) for more specific information.
You can checkout [`mathlib.hpp`](/src/mathlib.hpp) for more specific information.

## TODO
- [X] optimize temporary memory usage
1 change: 1 addition & 0 deletions examples/basic.mls
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ans = ((1 * 2 + 3) * 4) + 5 * 6 + 7
ans = (ans + 8) * (9 + 10)

ans
18 changes: 7 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
cmake_minimum_required(VERSION 3.10)

project(MathLang VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
option(DEBUG "Build with debug information" OFF)
option(PORTABLE "Build portable version" OFF)

add_executable("math-lang" main.cpp compiler.cpp runtime.cpp utils.cpp interpreter.cpp)
add_library(MathLangInterpreter compiler.cpp runtime.cpp utils.cpp interpreter.cpp)
add_executable("math-lang" main.cpp)
target_link_libraries("math-lang" MathLangInterpreter)
configure_file(config.hpp.in config.hpp)
target_include_directories("math-lang" PUBLIC ${PROJECT_BINARY_DIR})
target_compile_options("math-lang" PRIVATE -Wall)

if(DEBUG)
target_compile_definitions(MathLangInterpreter PRIVATE DEBUG)
target_compile_options(MathLangInterpreter PRIVATE -g)
target_compile_definitions("math-lang" PRIVATE DEBUG)
target_compile_options("math-lang" PRIVATE -g)
else()
target_compile_options(MathLangInterpreter PRIVATE -O3)
target_compile_options("math-lang" PRIVATE -O3)
endif()

if(PORTABLE)
target_compile_options("math-lang" PRIVATE -static)
target_link_options("math-lang" PRIVATE -static)
endif()
endif()
67 changes: 44 additions & 23 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <stack>
#include <cassert>
#include <cstdint>
#include "compiler.hpp"
#include "utils.hpp"
#include "mathlib.hpp"

using namespace MathLangUtils;

// Tokenizer
Tokenizer::Tokenizer() {
Expand Down Expand Up @@ -99,12 +98,14 @@ Parser::Parser() {
calc_list = std::vector<CalcStep>();
cmpl_res = CmplStat(CmplStat::Blank, "");
hash_oper = std::unordered_map<std::string, int>();
for(auto& oper : MathLangUtils::ALL_OPER) {
hash_oper[oper] = MathLangUtils::hash(oper);
for(auto& oper : Grammer::ALL_OPER) {
hash_oper[oper] = Hash::hash(oper);
}
aval_idnt_id = 0;
nidnt_table = std::unordered_map<std::string, int>();
idnt_table = std::unordered_map<std::string, int>();
aval_tmp_buffer_index = 0;
tmp_buffer = std::vector<int>();
}

Parser::Result_Ref Parser::get_parse_result() {
Expand Down Expand Up @@ -134,10 +135,21 @@ void Parser::merge_idnt_table() {
assert(nidnt_table.empty());
}

int Parser::apply_tmp_buffer() {
if(aval_tmp_buffer_index == tmp_buffer.size()) {
tmp_buffer.push_back(new_idnt_id());
}
return tmp_buffer[aval_tmp_buffer_index++];
}

void Parser::free_tmp_buffer() {
aval_tmp_buffer_index = 0;
}

Parser::CalcStep::Idnt Parser::store_pre_value() {
using Idnt = Parser::CalcStep::Idnt;
using Operator = Parser::CalcStep::Operator;
Idnt temp = Idnt::make_var(new_idnt_id());
Idnt temp = Idnt::make_var(apply_tmp_buffer());
calc_list.push_back(CalcStep(Operator::set, {temp, Idnt::make_pre_value()}));
return temp;
}
Expand All @@ -147,7 +159,6 @@ std::pair<bool, Parser::CalcStep::Idnt> Parser::sy_algo(
std::deque<Parser::CalcStep::Operator>& opers) {
using Idnt = Parser::CalcStep::Idnt;
using Operator = Parser::CalcStep::Operator;
using namespace MathLangUtils;

Debug::console << "sy_algo()\n";
int in_func = 0;
Expand Down Expand Up @@ -264,7 +275,7 @@ std::pair<bool, Parser::CalcStep::Idnt> Parser::sy_algo(
POP_TMP_TO_ORIGIN_DEQ();
continue;
}
if(!in_func && !opers.empty() && !idnts.empty() && OPER_RANK[top_oper] <= OPER_RANK[opers.back()]) {
if(!in_func && !opers.empty() && !idnts.empty() && Grammer::OPER_RANK[top_oper] <= Grammer::OPER_RANK[opers.back()]) {
ADD_DIV_TO_TMP_DEQ();
tmp_opers.push_back(top_oper);
// if top_idnt(ti) is PreValue, it will be covered by newest operation.
Expand Down Expand Up @@ -329,19 +340,19 @@ std::pair<CmplStat, Parser::Result_Ref> Parser::parse(const Tokenizer::Result_T&
// every bit represents expected operator or idnt
// the order of bit equals to the reverse order of ALL_OPER
// the last bit of it represents idnt
std::uint16_t expect_bits = 0b000001101;
DT::exprsye_t expect_bits = 0b000001101;
assert((expect_bits | ((1 << 10) - 1)) == (1 << 10) - 1);

#define IS_INVALID_TOKEN(rb) (((expect_bits | rb) ^ expect_bits) != 0)
#define TOKENS_TO_OPERATOR_MAPPING(tkr, oper, rb, eb) \
case hash_cxpr(tkr): {\
case Hash::hash_cxpr(tkr): {\
if(IS_INVALID_TOKEN(rb)) { \
Debug::console << "Invalid syntax detected!\n"; \
std::string expt; \
if(expect_bits & 1) expt.append("identifier "); \
for(int i = 1; i < 9; ++i) { \
if(expect_bits & (1 << i)) { \
expt.append(ALL_OPER[ALL_OPER_LEN - i]); \
expt.append(Grammer::ALL_OPER[Grammer::ALL_OPER_LEN - i]); \
expt.push_back(' '); \
} \
} \
Expand All @@ -355,7 +366,7 @@ std::pair<CmplStat, Parser::Result_Ref> Parser::parse(const Tokenizer::Result_T&
}

for(auto tk = tokens.begin(); tk != tokens.end(); ++tk) {
switch (hash(*tk)) {
switch (Hash::hash(*tk)) {
TOKENS_TO_OPERATOR_MAPPING("=", Operator::set, 0b100000000, 0b000001001)
TOKENS_TO_OPERATOR_MAPPING("+", Operator::plus, 0b010000000, 0b000001001)
TOKENS_TO_OPERATOR_MAPPING("*", Operator::multiply, 0b001000000, 0b000001001)
Expand All @@ -368,19 +379,19 @@ std::pair<CmplStat, Parser::Result_Ref> Parser::parse(const Tokenizer::Result_T&
if(IS_INVALID_TOKEN(0b000000001)) {
Debug::console << "Invalid syntax detected!\n";
std::string expt;
if(expect_bits & 1) expt.append("identifier "); \
if(expect_bits & 1) expt.append("identifier ");
for(int i = 1; i < 9; ++i) {
if(expect_bits & (1 << i)) {
expt.append(ALL_OPER[ALL_OPER_LEN - i]);
expt.append(Grammer::ALL_OPER[Grammer::ALL_OPER_LEN - i]);
expt.push_back(' ');
}
}
std::size_t position = tk - tokens.begin() + 1;
cmpl_res = CmplStat(CmplStat::Failed, "Syntax Error: expected { ", expt, "} at token position ", position, " found an identifier");
return {cmpl_res, pr_result};
}
if(is_str_number(*tk)) {
idnts.push_back(Idnt::make_raw(MathLangUtils::str_to_number(*tk)));
if(String::is_number(*tk)) {
idnts.push_back(Idnt::make_raw(String::to_number(*tk)));
expect_bits = 0b011110110;
break;
}
Expand All @@ -402,6 +413,7 @@ std::pair<CmplStat, Parser::Result_Ref> Parser::parse(const Tokenizer::Result_T&
auto [ok, res_idnt] = sy_algo(idnts, opers);
assert(opers.empty());
assert(idnts.empty());
free_tmp_buffer();
if(!ok) return {cmpl_res, pr_result};
if(calc_list.empty()) {
calc_list.push_back(CalcStep(Operator::print, {res_idnt}));
Expand All @@ -428,11 +440,11 @@ std::ostream& operator<<(std::ostream& os, const Parser& p) {
os << k << ": " << v << '\n';
os << "calc_list:\n";
for(auto& i : p.calc_list) {
os << MathLangUtils::ALL_OPER_NAMES[i.oper] << " [" ;
os << Grammer::ALL_OPER_NAMES[i.oper] << " [" ;
for(auto& j : i.idnts) {
switch(j.idnt_type) {
case Idnt::Raw:
os << j.raw_value << ", ";
os << j.raw_value_const() << ", ";
break;
case Idnt::None:
os << "None" << ", ";
Expand All @@ -441,10 +453,10 @@ std::ostream& operator<<(std::ostream& os, const Parser& p) {
os << "PreValue" << ", ";
break;
case Idnt::Var:
os << "Var(" << j.idnt_id << ")" << ", ";
os << "Var(" << j.idnt_id_const() << ")" << ", ";
break;
case Idnt::Func:
os << "Func(" << j.idnt_id << ")" << ", ";
os << "Func(" << j.idnt_id_const() << ")" << ", ";
break;
}
}
Expand All @@ -459,6 +471,7 @@ std::ostream& operator<<(std::ostream& os, const Parser& p) {
for(auto& [k, v] : p.nidnt_table)
os << k << ": " << v << '\n';
os << "aval_idnt_id: " << p.aval_idnt_id << '\n';
os << "tmp_buffer.size(): " << p.tmp_buffer.size() << '\n';
os << "[Parser END]";
return os;
}
Expand All @@ -468,14 +481,22 @@ Compiler::Compiler() {}

std::pair<CmplStat, const Compiler::CmplResult&> Compiler::compile(const std::string& sline) {
auto tokens = tokenizer.tokenize(sline);
Debug::console << tokenizer << '\n';
if(tokens.empty()) {
Debug::console << "User input length is 0, stop at tokenization\n";
auto cmpl_stat = CmplStat(CmplStat::Empty, "Empty line");
auto cmpl_res = CmplResult::make_result(nullptr);
return {cmpl_stat, cmpl_res};
}
auto ret = parser.parse(tokens);
Debug::console << parser << '\n';
return ret;
}
}

#ifdef DEBUG
std::ostream& operator<<(std::ostream& os, const Compiler& c) {
os << "[Compiler]:\n";
os << c.tokenizer << '\n';
os << c.parser << '\n';
os << "[Compiler END]";
return os;
}
#endif
Loading
Loading