Skip to content

Commit

Permalink
feat(parser,tokens): syntax sugar - struct is same as @OnStack class
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Jun 24, 2023
1 parent c29f99c commit a40e242
Show file tree
Hide file tree
Showing 28 changed files with 144 additions and 132 deletions.
2 changes: 1 addition & 1 deletion compiler/scripts/update_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"and", "continue", "for", "try",
"as", "def", "from", "while",
"assert", "del", "not",
"elif", "if", "or", "defer", "ccode", "runtimefeature", "in"])
"elif", "if", "or", "defer", "ccode", "runtimefeature", "in", "struct"])
TOKENS = sorted([
"NAME", "AT", "DOUBLE_NUMBER", "FLOAT_NUMBER", "INDENT", "BA_INDENT", "BA_DEDENT", "NEW_LINE", "COLON", "COMMENT",
"THREE_QUOTE_STRING", "STRING", "PAREN_OPEN",
Expand Down
26 changes: 17 additions & 9 deletions compiler/src/ast/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,12 @@ void parser::verify_statements(token *token, std::vector<stmt *> &statements) {
void parser::verify_only_single_line_statements(
token *token, std::vector<stmt *> &statements) {
for (stmt *st : statements) {
if (!(st->get_type() == ast_type::STMT_DEFER
|| st->get_type() == ast_type::STMT_DEL
|| st->get_type() == ast_type::STMT_PASS
|| st->get_type() == ast_type::STMT_RETURN
|| st->get_type() == ast_type::STMT_EXPRESSION
|| st->get_type() == ast_type::STMT_CCODE
)) {
if (!(st->get_type() == ast_type::STMT_DEFER ||
st->get_type() == ast_type::STMT_DEL ||
st->get_type() == ast_type::STMT_PASS ||
st->get_type() == ast_type::STMT_RETURN ||
st->get_type() == ast_type::STMT_EXPRESSION ||
st->get_type() == ast_type::STMT_CCODE)) {
throw error(token,
"Blocks with nested import/def/class is not supported");
}
Expand Down Expand Up @@ -424,7 +423,9 @@ stmt *parser::declaration_statement() {
return runtimefeature_statement();
}
if (match({token_type::KEYWORD_DEF})) { return def_statement({}); }
if (match({token_type::KEYWORD_CLASS})) { return class_statement({}); }
if (match({token_type::KEYWORD_CLASS, token_type::KEYWORD_STRUCT})) {
return class_statement({});
}
if (match({token_type::AT})) { return attempt_parse_def_or_class(); }
if (!match({token_type::NAME})) { return statement(); }
var_name = previous();
Expand Down Expand Up @@ -666,6 +667,13 @@ ykdatatype *parser::parse_datatype() {
return dt;
}
stmt *parser::class_statement(annotations ants) {
auto class_token = previous();
if (class_token->type_ == token_type::KEYWORD_STRUCT) {
if (ants.on_stack_) {
throw error(class_token, "struct is already @onstack");
}
ants.on_stack_ = true;
}
auto name = consume(token_type::NAME, "Class name must be present");
consume(token_type::COLON, "Colon must be present after class name");
consume(token_type::NEW_LINE, "Class block must start with a new line");
Expand Down Expand Up @@ -719,7 +727,7 @@ stmt *parser::attempt_parse_def_or_class() {
if (!ants.error_.empty()) { throw error(at_sign, ants.error_); }
} else if (match({token_type::KEYWORD_DEF})) {
return def_statement(ants);
} else if (match({token_type::KEYWORD_CLASS})) {
} else if (match({token_type::KEYWORD_CLASS, token_type::KEYWORD_STRUCT})) {
return class_statement(ants);
} else {
break;
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/ast/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ namespace yaksha {
parsing_error error(token *tok, const std::string &message);
void handle_error(const parsing_error &err);
void synchronize_parser();
void verify_statements(token* t, std::vector<stmt*>& statements);
void verify_only_single_line_statements(token* token, std::vector<stmt *>&statements);
void verify_statements(token *t, std::vector<stmt *> &statements);
void verify_only_single_line_statements(token *token,
std::vector<stmt *> &statements);
// state
std::size_t current_;
ast_pool pool_;
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/carpntr_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ std::string get_my_exe_path() {
if (path == nullptr) { return nullptr; }
wai_getModulePath(path, length, nullptr);
path[length] = '\0';
return std::string{path, static_cast<std::basic_string<char>::size_type>(length)};
return std::string{path,
static_cast<std::basic_string<char>::size_type>(length)};
}
#endif
3 changes: 2 additions & 1 deletion compiler/src/compiler/desugaring_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ void desugaring_compiler::visit_foreach_stmt(foreach_stmt *obj) {
// While body -> expose yy__item ==> obj->name + obj->data_type
const std::string desugar_rewrite = "(" + array_holder + "[" + counter + "])";
new_while_body.emplace_back(ast_pool_->c_compins_stmt(
obj->name_, obj->data_type_, create_str_literal(desugar_rewrite), nullptr, nullptr));
obj->name_, obj->data_type_, create_str_literal(desugar_rewrite), nullptr,
nullptr));
// Create counter += 1 statement
auto counter_incr = ast_pool_->c_expression_stmt(ast_pool_->c_assign_expr(
counter_tok, plus_eq_token_,
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/compiler/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,11 @@ void type_checker::handle_assigns(token *oper, const ykobject &lhs,
}
if ((lhs.is_primitive_or_obj() && rhs.is_primitive_or_obj())) {
auto rhs_dt = rhs.datatype_;
if (rhs_dt->is_const()) {
rhs_dt = rhs.datatype_->args_[0];
}
if (rhs_dt->is_const()) { rhs_dt = rhs.datatype_->args_[0]; }
if (*lhs.datatype_ != *rhs_dt) {
error(oper, "Cannot assign between 2 different data types.");
}
}

token_type operator_type = oper->type_;
switch (operator_type) {
case token_type::AND_EQ:
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/file_formats/ic_tokens_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include <vector>
using namespace yaksha;
bool yaksha::ic_save_token_dump(const std::string &file,
const std::vector<ic_token> &tokens) {
const std::vector<ic_token> &tokens) {
std::ofstream save_file(file);
if (!save_file.is_open()) { return false; }
ic_write_token_dump(save_file, tokens);
return true;
}
void yaksha::ic_write_token_dump(std::ostream &output,
const std::vector<ic_token> &tokens) {
const std::vector<ic_token> &tokens) {
bool comma = false;
output << "[";
for (auto &tok : tokens) {
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/file_formats/ic_tokens_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace yaksha {
std::vector<ic_token> ic_load_token_dump(const std::string &file);
[[maybe_unused]] bool ic_save_token_dump(const std::string &file,
const std::vector<ic_token> &tokens);
void ic_write_token_dump(std::ostream &output, const std::vector<ic_token> &tokens);
const std::vector<ic_token> &tokens);
void ic_write_token_dump(std::ostream &output,
const std::vector<ic_token> &tokens);
}// namespace yaksha
#endif
3 changes: 1 addition & 2 deletions compiler/src/ic2c/ic_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ ic_stmt *ic_parser::define_st(ic_token *hash_t) {
std::vector<ic_token *> tok_string = token_string();
consume(ic_token_type::NEWLINE, "new line is expected after #define");
return ast_pool_->ic_c_pp_define_function_stmt(
hash_t, define_tok, identifier_tok,
paren_open, args, paren_close,
hash_t, define_tok, identifier_tok, paren_open, args, paren_close,
tok_string);
}
// parse simple #define
Expand Down
22 changes: 11 additions & 11 deletions compiler/src/ic2c/ic_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace yaksha {

private:
// ---------------------- Parsing ----------------------
ic_stmt* preprocessor_statement();
ic_stmt* define_st(ic_token* hash_t);
ic_stmt* include_st(ic_token* hash_t);
ic_stmt* line_st(ic_token* hash_t);
ic_stmt* undef_st(ic_token* hash_t);
ic_stmt* error_st(ic_token* hash_t);
ic_stmt* warning_st(ic_token* hash_t);
ic_stmt* pragma_st(ic_token* hash_t);
ic_stmt* code_line();
ic_stmt* block(ic_token* hash_t);
ic_stmt* if_st(ic_token* hash_t);
ic_stmt *preprocessor_statement();
ic_stmt *define_st(ic_token *hash_t);
ic_stmt *include_st(ic_token *hash_t);
ic_stmt *line_st(ic_token *hash_t);
ic_stmt *undef_st(ic_token *hash_t);
ic_stmt *error_st(ic_token *hash_t);
ic_stmt *warning_st(ic_token *hash_t);
ic_stmt *pragma_st(ic_token *hash_t);
ic_stmt *code_line();
ic_stmt *block(ic_token *hash_t);
ic_stmt *if_st(ic_token *hash_t);
// ---------------------- State -------------------------
ic_ast_pool *ast_pool_;
std::vector<ic_token> &tokens_;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/ic2c/ic_peek_ahead_iter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ic_peek_ahead_iter.cpp
#include "ic_peek_ahead_iter.h"
using namespace yaksha;
ic_peek_ahead_iter::ic_peek_ahead_iter(ic_simple_character_iter &tt)
ic_peek_ahead_iter::ic_peek_ahead_iter(ic_simple_character_iter &tt)
: tt_(tt), current_(0), next_(0), after_next_(0), fourth_(0) {
current_ = tt_.get_current();
current_l_ = tt_.get_line();
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/ic2c/ic_peek_ahead_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ namespace yaksha {
int fourth_l_;
bool fourth_e_;
};
}
}// namespace yaksha
#endif
2 changes: 1 addition & 1 deletion compiler/src/ic2c/ic_simple_character_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ namespace yaksha {
virtual int get_line() = 0;
virtual ~ic_simple_character_iter() = default;
};
}
}// namespace yaksha
#endif
8 changes: 4 additions & 4 deletions compiler/src/ic2c/ic_tokenizer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ic_tokenizer.h
#ifndef IC_TOKENIZER_H
#define IC_TOKENIZER_H
#include <string>
#include <vector>
#include "ic_line_splicer.h"
#include "ic_peek_ahead_iter.h"
#include "ic_token.h"
#include "ic_trigraph_translater.h"
#include "ic_peek_ahead_iter.h"
#include "ic_line_splicer.h"
#include <string>
#include <vector>
namespace yaksha {
struct ic_token;
struct ic_parsing_error;
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/ic2c/ic_trigraph_translater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void ic_trigraph_translater::read() {
utf8::uint32_t next = std::get<1>(characters);
utf8::uint32_t after_next = std::get<2>(characters);
bool read_skip_ = false;
if ((current == '\r' && next == '\n') || current == '\r' || (current == '\n' && prev != '\r')) {
if ((current == '\r' && next == '\n') || current == '\r' ||
(current == '\n' && prev != '\r')) {
increment_line_ = true;
}
if (current == '?' && next == '?') {
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/tokenizer/block_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ void block_analyzer::analyze() {
if (tok.type_ == token_type::NEW_LINE) {
// Don't start with new lines, Don't add a newline after a new line
if (prev_comment) {
if (cleaned.empty() || cleaned.back().line_ != tok.line_) {
continue;
}
if (cleaned.empty() || cleaned.back().line_ != tok.line_) {
continue;
}
}
if (cleaned.empty() ||
cleaned.back().type_ == token_type::NEW_LINE) {
if (cleaned.empty() || cleaned.back().type_ == token_type::NEW_LINE) {
continue;
} else if (cleaned.back().type_ == token_type::INDENT) {
// get rid of empty indents that just follows by a new line
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/tokenizer/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace yaksha {
KEYWORD_PASS,
KEYWORD_RETURN,
KEYWORD_RUNTIMEFEATURE,
KEYWORD_STRUCT,
KEYWORD_TRY,
KEYWORD_WHILE,
TK_UNKNOWN_TOKEN_DETECTED
Expand Down Expand Up @@ -253,6 +254,7 @@ namespace yaksha {
if (t == token_type::KEYWORD_RETURN) return "KEYWORD_RETURN";
if (t == token_type::KEYWORD_RUNTIMEFEATURE)
return "KEYWORD_RUNTIMEFEATURE";
if (t == token_type::KEYWORD_STRUCT) return "KEYWORD_STRUCT";
if (t == token_type::KEYWORD_TRY) return "KEYWORD_TRY";
if (t == token_type::KEYWORD_WHILE) return "KEYWORD_WHILE";
return "TK_UNKNOWN_TOKEN_DETECTED";
Expand Down Expand Up @@ -375,6 +377,7 @@ namespace yaksha {
if (t == "KEYWORD_RETURN") return token_type::KEYWORD_RETURN;
if (t == "KEYWORD_RUNTIMEFEATURE")
return token_type::KEYWORD_RUNTIMEFEATURE;
if (t == "KEYWORD_STRUCT") return token_type::KEYWORD_STRUCT;
if (t == "KEYWORD_TRY") return token_type::KEYWORD_TRY;
if (t == "KEYWORD_WHILE") return token_type::KEYWORD_WHILE;
return token_type::TK_UNKNOWN_TOKEN_DETECTED;
Expand Down Expand Up @@ -405,6 +408,7 @@ namespace yaksha {
if (t == "pass") return token_type::KEYWORD_PASS;
if (t == "return") return token_type::KEYWORD_RETURN;
if (t == "runtimefeature") return token_type::KEYWORD_RUNTIMEFEATURE;
if (t == "struct") return token_type::KEYWORD_STRUCT;
if (t == "try") return token_type::KEYWORD_TRY;
if (t == "while") return token_type::KEYWORD_WHILE;
return token_type::TK_UNKNOWN_TOKEN_DETECTED;
Expand Down
12 changes: 6 additions & 6 deletions compiler/test_data/compiler_tests/casting_test.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
["output.c",2,26,"-","SUB"],
["output.c",2,27,"-","SUB"],
["output.c",2,29,"\n","NEW_LINE"],
["output.c",3,1,"struct","NAME"],
["output.c",3,1,"struct","KEYWORD_STRUCT"],
["output.c",3,8,"yy__B","NAME"],
["output.c",3,14,"\n","NEW_LINE"],
["output.c",4,1,"define yy__A void*","COMMENT"],
["output.c",4,20,"\n","NEW_LINE"],
["output.c",5,1,"yy__A","NAME"],
["output.c",5,7,"yy__toA","NAME"],
["output.c",5,14,"(","PAREN_OPEN"],
["output.c",5,15,"struct","NAME"],
["output.c",5,15,"struct","KEYWORD_STRUCT"],
["output.c",5,22,"yy__B","NAME"],
["output.c",5,27,"*","MUL"],
["output.c",5,28,")","PAREN_CLOSE"],
Expand All @@ -37,7 +37,7 @@
["output.c",7,13,"-","SUB"],
["output.c",7,14,"-","SUB"],
["output.c",7,16,"\n","NEW_LINE"],
["output.c",8,1,"struct","NAME"],
["output.c",8,1,"struct","KEYWORD_STRUCT"],
["output.c",8,8,"yy__B","NAME"],
["output.c",8,14,"{","CURLY_BRACKET_OPEN"],
["output.c",8,15,"\n","NEW_LINE"],
Expand All @@ -57,7 +57,7 @@
["output.c",12,1,"yy__A","NAME"],
["output.c",12,7,"yy__toA","NAME"],
["output.c",12,14,"(","PAREN_OPEN"],
["output.c",12,15,"struct","NAME"],
["output.c",12,15,"struct","KEYWORD_STRUCT"],
["output.c",12,22,"yy__B","NAME"],
["output.c",12,27,"*","MUL"],
["output.c",12,29,"yy__b","NAME"],
Expand Down Expand Up @@ -90,7 +90,7 @@
["output.c",18,1,"{","CURLY_BRACKET_OPEN"],
["output.c",18,2,"\n","NEW_LINE"],
["output.c",19,1," ","INDENT"],
["output.c",19,5,"struct","NAME"],
["output.c",19,5,"struct","KEYWORD_STRUCT"],
["output.c",19,12,"yy__B","NAME"],
["output.c",19,17,"*","MUL"],
["output.c",19,19,"yy__b","NAME"],
Expand All @@ -101,7 +101,7 @@
["output.c",19,35,",","COMMA"],
["output.c",19,37,"sizeof","NAME"],
["output.c",19,43,"(","PAREN_OPEN"],
["output.c",19,44,"struct","NAME"],
["output.c",19,44,"struct","KEYWORD_STRUCT"],
["output.c",19,51,"yy__B","NAME"],
["output.c",19,56,")","PAREN_CLOSE"],
["output.c",19,57,")","PAREN_CLOSE"],
Expand Down
16 changes: 8 additions & 8 deletions compiler/test_data/compiler_tests/class_stuff.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
["output.c",2,26,"-","SUB"],
["output.c",2,27,"-","SUB"],
["output.c",2,29,"\n","NEW_LINE"],
["output.c",3,1,"struct","NAME"],
["output.c",3,1,"struct","KEYWORD_STRUCT"],
["output.c",3,8,"yy__A","NAME"],
["output.c",3,14,"\n","NEW_LINE"],
["output.c",4,1,"struct","NAME"],
["output.c",4,1,"struct","KEYWORD_STRUCT"],
["output.c",4,8,"yy__B","NAME"],
["output.c",4,14,"\n","NEW_LINE"],
["output.c",5,1,"int32_t","NAME"],
Expand All @@ -30,7 +30,7 @@
["output.c",6,13,"-","SUB"],
["output.c",6,14,"-","SUB"],
["output.c",6,16,"\n","NEW_LINE"],
["output.c",7,1,"struct","NAME"],
["output.c",7,1,"struct","KEYWORD_STRUCT"],
["output.c",7,8,"yy__A","NAME"],
["output.c",7,14,"{","CURLY_BRACKET_OPEN"],
["output.c",7,15,"\n","NEW_LINE"],
Expand All @@ -40,7 +40,7 @@
["output.c",8,19,"\n","NEW_LINE"],
["output.c",9,1,"}","CURLY_BRACKET_CLOSE"],
["output.c",9,3,"\n","NEW_LINE"],
["output.c",10,1,"struct","NAME"],
["output.c",10,1,"struct","KEYWORD_STRUCT"],
["output.c",10,8,"yy__B","NAME"],
["output.c",10,14,"{","CURLY_BRACKET_OPEN"],
["output.c",10,15,"\n","NEW_LINE"],
Expand All @@ -65,7 +65,7 @@
["output.c",15,1,"{","CURLY_BRACKET_OPEN"],
["output.c",15,2,"\n","NEW_LINE"],
["output.c",16,1," ","INDENT"],
["output.c",16,5,"struct","NAME"],
["output.c",16,5,"struct","KEYWORD_STRUCT"],
["output.c",16,12,"yy__A","NAME"],
["output.c",16,17,"*","MUL"],
["output.c",16,19,"yy__a","NAME"],
Expand All @@ -76,7 +76,7 @@
["output.c",16,35,",","COMMA"],
["output.c",16,37,"sizeof","NAME"],
["output.c",16,43,"(","PAREN_OPEN"],
["output.c",16,44,"struct","NAME"],
["output.c",16,44,"struct","KEYWORD_STRUCT"],
["output.c",16,51,"yy__A","NAME"],
["output.c",16,56,")","PAREN_CLOSE"],
["output.c",16,57,")","PAREN_CLOSE"],
Expand All @@ -92,7 +92,7 @@
["output.c",17,29,")","PAREN_CLOSE"],
["output.c",17,31,"\n","NEW_LINE"],
["output.c",18,1," ","INDENT"],
["output.c",18,5,"struct","NAME"],
["output.c",18,5,"struct","KEYWORD_STRUCT"],
["output.c",18,12,"yy__B","NAME"],
["output.c",18,17,"*","MUL"],
["output.c",18,19,"yy__b","NAME"],
Expand All @@ -103,7 +103,7 @@
["output.c",18,35,",","COMMA"],
["output.c",18,37,"sizeof","NAME"],
["output.c",18,43,"(","PAREN_OPEN"],
["output.c",18,44,"struct","NAME"],
["output.c",18,44,"struct","KEYWORD_STRUCT"],
["output.c",18,51,"yy__B","NAME"],
["output.c",18,56,")","PAREN_CLOSE"],
["output.c",18,57,")","PAREN_CLOSE"],
Expand Down
Loading

0 comments on commit a40e242

Please sign in to comment.