Skip to content

Commit

Permalink
lexer: Add '!' (bang) as symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Oct 5, 2020
1 parent 2d6fba8 commit ef6cf3b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
38 changes: 37 additions & 1 deletion nyan/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ const std::vector<ASTImport> &AST::get_imports() const {
AST::AST(TokenStream &tokens) {
while (tokens.full()) {
auto token = tokens.next();
if (token->type == token_type::IMPORT) {
if (token->type == token_type::BANG) {
this->args.emplace_back(tokens);
}
else if (token->type == token_type::IMPORT) {
this->imports.emplace_back(tokens);
}
else if (token->type == token_type::ID) {
Expand All @@ -96,6 +99,33 @@ AST::AST(TokenStream &tokens) {
}


ASTArgument::ASTArgument(TokenStream &tokens) {
auto token = tokens.next();

if (token->type == token_type::ID) {
this->arg = IDToken{*token, tokens};
token = tokens.next();
} else {
throw ASTError("expected parameter keyword, encountered", *token);
}

while (token->type != token_type::ENDLINE) {
this->params.emplace_back(*token, tokens);
token = tokens.next();
}
}


const IDToken &ASTArgument::get_arg() const {
return this->arg;
}


const std::vector<IDToken> &ASTArgument::get_params() const {
return this->params;
}


ASTImport::ASTImport(TokenStream &tokens) {
auto token = tokens.next();

Expand Down Expand Up @@ -560,6 +590,12 @@ void AST::strb(std::ostringstream &builder, int indentlevel) const {
}
}

void ASTArgument::strb(std::ostringstream &builder, int /*indentlevel*/) const {
builder << "!" << this->arg.str();
for (auto &param : this->params) {
builder << " " << param.str();
}
}

void ASTImport::strb(std::ostringstream &builder, int /*indentlevel*/) const {
builder << "import " << this->namespace_name.str();
Expand Down
24 changes: 23 additions & 1 deletion nyan/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ class ASTMember : public ASTBase {
};


/**
* The abstract syntax tree representation of an argument.
*/
class ASTArgument : public ASTBase {
public:
ASTArgument(TokenStream &tokens);

void strb(std::ostringstream &builder, int indentlevel=0) const override;

/** return the payload */
const IDToken &get_arg() const;

/** return the payload */
const std::vector<IDToken> &get_params() const;

protected:
IDToken arg;
std::vector<IDToken> params;
};


/**
* An import in a nyan file is represented by this AST entry.
* Used for the `import ... (as ...)` statement.
Expand All @@ -142,7 +163,7 @@ class ASTImport : public ASTBase {


/**
* Inheritance chang
* Inheritance change
*/
class ASTInheritanceChange : public ASTBase {
friend class Database;
Expand Down Expand Up @@ -200,6 +221,7 @@ class AST : public ASTBase {
const std::vector<ASTImport> &get_imports() const;

protected:
std::vector<ASTArgument> args;
std::vector<ASTImport> imports;
std::vector<ASTObject> objects;
};
Expand Down
4 changes: 3 additions & 1 deletion nyan/basic_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum class primitive_t {
INT,
FLOAT,
CONTAINER,
OBJECT
OBJECT,
};


Expand All @@ -34,6 +34,7 @@ enum class container_t {
SINGLE,
SET,
ORDEREDSET,
DICT,
};


Expand Down Expand Up @@ -122,6 +123,7 @@ constexpr const char *container_type_to_string(container_t type) {
case container_t::SINGLE: return "single_value";
case container_t::SET: return "set";
case container_t::ORDEREDSET: return "orderedset";
case container_t::DICT: return "dict";
}

return "unhandled container_t";
Expand Down
1 change: 1 addition & 0 deletions nyan/lexer/flex.lpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ float -?({digit}+\.{digit}*|{digit}*\.{digit}+)
"{" { impl->token(nyan::token_type::LBRACE); }
"}" { impl->token(nyan::token_type::RBRACE); }
"@" { impl->token(nyan::token_type::AT); }
"!" { impl->token(nyan::token_type::BANG); }

"pass" { impl->token(nyan::token_type::PASS); }
"..." { impl->token(nyan::token_type::ELLIPSIS); }
Expand Down
2 changes: 2 additions & 0 deletions nyan/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class File;
enum class token_type {
AS,
AT,
BANG,
COLON,
COMMA,
DEDENT,
Expand Down Expand Up @@ -65,6 +66,7 @@ constexpr const char *token_type_str(token_type type) {
switch (type) {
case token_type::AS: return "as";
case token_type::AT: return "@";
case token_type::BANG: return "!";
case token_type::COLON: return "colon";
case token_type::COMMA: return "comma";
case token_type::DEDENT: return "dedentation";
Expand Down
2 changes: 2 additions & 0 deletions test/test.nyan
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# test nyan file
!version 1
!arg 1 2 3 "Test"

Dummy():
...
Expand Down

0 comments on commit ef6cf3b

Please sign in to comment.