Skip to content

Commit

Permalink
removed obsolete block AST node
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBendun committed Jun 9, 2023
1 parent e5bc5cb commit 2f87e63
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 168 deletions.
117 changes: 5 additions & 112 deletions musique/interpreter/builtin_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -643,36 +643,6 @@ static Result<Value> builtin_primes(Interpreter&, std::vector<Value> args)
};
}

//: Funkcja `nprimes` zwraca zadaną liczbę kolejnych liczb pierwszych.
//:
//: # Przykład
//: ```
//: > for (nprimes 4) (say)
//: 2
//: 3
//: 5
//: 7
//: ```
/// Iterate over container
static Result<Value> builtin_for(Interpreter &i, std::vector<Value> args)
{
constexpr auto guard = Guard<1> {
.name = "for",
.possibilities = { "(array, callback) -> any" }
};

if (auto a = match<Collection, Function>(args)) {
auto& [collection, func] = *a;
Value result{};
for (size_t n = 0; n < collection.size(); ++n) {
result = Try(func(i, { Try(collection.index(i, n)) }));
}
return result;
} else {
return guard.yield_error();
}
}

//: Funkcja `fold` TODO.
//:
//: # Przykład
Expand Down Expand Up @@ -772,85 +742,6 @@ static Result<Value> builtin_scan(Interpreter &interpreter, std::vector<Value> a
};
}

//: Funkcja `if` wykonuje określony blok po spełnieniu warunku, alternatywnie wykonuje inny blok kodu w przeciwnym wypadku.
//:
//: # Przykład
//: ```
//: > if true (say 42)
//: 42
//: > if false (say 42)
//: >
//: > if true (say 42) (say 0)
//: 42
//: > if false (say 42) (say 0)
//: 0
//: ```
/// Execute blocks depending on condition
static Result<Value> builtin_if(Interpreter &i, std::span<Ast> args) {
static constexpr auto guard = Guard<2> {
.name = "if",
.possibilities = {
"(any, function) -> any",
"(any, function, function) -> any"
}
};

if (args.size() != 2 && args.size() != 3) {
return guard.yield_error();
}

if (Try(i.eval((Ast)args.front())).truthy()) {
if (args[1].type == Ast::Type::Block) {
return Try(i.eval((Ast)args[1].arguments.front()));
} else {
return Try(i.eval((Ast)args[1]));
}
} else if (args.size() == 3) {
if (args[2].type == Ast::Type::Block) {
return Try(i.eval((Ast)args[2].arguments.front()));
} else {
return Try(i.eval((Ast)args[2]));
}
}

return Value{};
}

//: Funkcja `while` wykonuje określony blok dopóki warunek jest spełniony.
//:
//: # Przykład
//: ```
//: > i := 0
//: > while (i < 10) (say i, i += 2)
//: 0
//: 2
//: 4
//: 6
//: 8
//: ```
/// Loop block depending on condition
static Result<Value> builtin_while(Interpreter &i, std::span<Ast> args) {
static constexpr auto guard = Guard<2> {
.name = "while",
.possibilities = {
"(any, function) -> any"
}
};

if (args.size() != 2) {
return guard.yield_error();
}

while (Try(i.eval((Ast)args.front())).truthy()) {
if (args[1].type == Ast::Type::Block) {
Try(i.eval((Ast)args[1].arguments.front()));
} else {
Try(i.eval((Ast)args[1]));
}
}
return Value{};
}

//: Funkcja `try` przystępuje do wykonania bloków kodu, a jeżeli którykolwiek z nich zakończy się niepowodzeniem, wykonuje ostatni. Jeżeli ostatni też zakończy się niepowodzeniem, to trudno.
//:
//: # Przykład
Expand All @@ -868,6 +759,10 @@ static Result<Value> builtin_while(Interpreter &i, std::span<Ast> args) {
/// Try executing all but last block and if it fails execute last one
static Result<Value> builtin_try(Interpreter &interpreter, std::span<Ast> args)
{
(void)interpreter;
(void)args;
unimplemented();
#if 0
if (args.size() == 1) {
// TODO This should be abstracted
auto result = (args[0].type == Ast::Type::Block)
Expand All @@ -894,6 +789,7 @@ static Result<Value> builtin_try(Interpreter &interpreter, std::span<Ast> args)
}

return success;
#endif
}

//: Funkcja `update` aktualizuje dany element listy na nową wartość.
Expand Down Expand Up @@ -1760,9 +1656,7 @@ void Interpreter::register_builtin_functions()
global.force_define("flat", builtin_flat);
global.force_define("floor", builtin_floor);
global.force_define("fold", builtin_fold);
global.force_define("for", builtin_for);
global.force_define("hash", builtin_hash);
global.force_define("if", builtin_if);
global.force_define("instrument", builtin_program_change);
global.force_define("len", builtin_len);
global.force_define("map", builtin_map);
Expand Down Expand Up @@ -1801,5 +1695,4 @@ void Interpreter::register_builtin_functions()
global.force_define("unique", builtin_unique);
global.force_define("up", builtin_up);
global.force_define("update", builtin_update);
global.force_define("while", builtin_while);
}
19 changes: 5 additions & 14 deletions musique/interpreter/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,14 @@ Result<Value> Interpreter::eval(Ast &&ast)
}
}

case Ast::Type::Block:
case Ast::Type::Lambda:
{
Block block;
if (ast.type == Ast::Type::Lambda) {
auto parameters = std::span<Ast>(ast.arguments.data(), ast.arguments.size() - 1);
block.parameters.reserve(parameters.size());
for (auto &param : parameters) {
ensure(param.type == Ast::Type::Literal && param.token.type == Token::Type::Symbol, "Not a name in parameter section of Ast::lambda");
block.parameters.push_back(std::string(std::move(param).token.source));
}
auto parameters = std::span<Ast>(ast.arguments.data(), ast.arguments.size() - 1);
block.parameters.reserve(parameters.size());
for (auto &param : parameters) {
ensure(param.type == Ast::Type::Literal && param.token.type == Token::Type::Symbol, "Not a name in parameter section of Ast::lambda");
block.parameters.push_back(std::string(std::move(param).token.source));
}

block.context = env;
Expand Down Expand Up @@ -402,12 +399,6 @@ static void snapshot(std::ostream &out, Ast const& ast) {
out << ", ";
}
}
break; case Ast::Type::Block:
ensure(ast.arguments.size() == 1, "Block can contain only one node which contains its body");
out << "(";
snapshot(out, ast.arguments.front());
out << ")";

break; case Ast::Type::Lambda:
out << "(";
for (auto i = 0u; i+1 < ast.arguments.size(); ++i) {
Expand Down
24 changes: 12 additions & 12 deletions musique/lexer/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ auto Lexer::next_token() -> Result<std::variant<Token, End_Of_File>>
}

switch (peek()) {
case '(': consume(); return Token { Token::Type::Bra, token_location, finish() };
case ')': consume(); return Token { Token::Type::Ket, token_location, finish() };
case '[': consume(); return Token { Token::Type::Open_Index, token_location, finish() };
case ']': consume(); return Token { Token::Type::Close_Index, token_location, finish() };
case '(': consume(); return Token { Token::Type::Open_Paren, token_location, finish() };
case ')': consume(); return Token { Token::Type::Close_Paren, token_location, finish() };
case '[': consume(); return Token { Token::Type::Open_Bracket, token_location, finish() };
case ']': consume(); return Token { Token::Type::Close_Bracket, token_location, finish() };
case ',': consume(); return Token { Token::Type::Comma, token_location, finish() };
case '\n': consume(); return Token { Token::Type::Nl, token_location, finish() };

Expand Down Expand Up @@ -282,15 +282,15 @@ std::ostream& operator<<(std::ostream& os, Token const& token)
std::ostream& operator<<(std::ostream& os, Token::Type type)
{
switch (type) {
case Token::Type::Bra: return os << "BRA";
case Token::Type::Open_Paren: return os << "OPEN PAREN";
case Token::Type::Chord: return os << "CHORD";
case Token::Type::Close_Index: return os << "CLOSE INDEX";
case Token::Type::Close_Bracket: return os << "CLOSE BRACKET";
case Token::Type::Comma: return os << "COMMA";
case Token::Type::Ket: return os << "KET";
case Token::Type::Close_Paren: return os << "CLOSE PAREN";
case Token::Type::Keyword: return os << "KEYWORD";
case Token::Type::Nl: return os << "NL";
case Token::Type::Numeric: return os << "NUMERIC";
case Token::Type::Open_Index: return os << "OPEN INDEX";
case Token::Type::Open_Bracket: return os << "OPEN BRACKET";
case Token::Type::Operator: return os << "OPERATOR";
case Token::Type::Parameter_Separator: return os << "PARAMETER SEPARATOR";
case Token::Type::Symbol: return os << "SYMBOL";
Expand All @@ -301,15 +301,15 @@ std::ostream& operator<<(std::ostream& os, Token::Type type)
std::string_view type_name(Token::Type type)
{
switch (type) {
case Token::Type::Bra: return "(";
case Token::Type::Open_Paren: return "(";
case Token::Type::Chord: return "chord";
case Token::Type::Close_Index: return "]";
case Token::Type::Close_Bracket: return "]";
case Token::Type::Comma: return ",";
case Token::Type::Ket: return ")";
case Token::Type::Close_Paren: return ")";
case Token::Type::Keyword: return "keyword";
case Token::Type::Nl: return "newline";
case Token::Type::Numeric: return "numeric";
case Token::Type::Open_Index: return "[";
case Token::Type::Open_Bracket: return "[";
case Token::Type::Operator: return "operator";
case Token::Type::Parameter_Separator: return "parameter separator";
case Token::Type::Symbol: return "symbol";
Expand Down
8 changes: 4 additions & 4 deletions musique/lexer/token.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ struct Token
Parameter_Separator, ///< "|" separaters arguments from block body
Comma, ///< "," separates expressions. Used mainly to separate calls, like `foo 1 2; bar 3 4`
Nl, ///< "\n" seperates expressions similar to Comma
Bra, ///< "(" starts anonymous block of code (potentially a function)
Ket, ///< ")" ends anonymous block of code (potentially a function)
Open_Index, ///< "[" starts index section of index expression
Close_Index ///< "]" ends index section of index expression
Open_Paren, ///< "(" starts anonymous block of code (potentially a function)
Close_Paren, ///< ")" ends anonymous block of code (potentially a function)
Open_Bracket, ///< "[" starts index section of index expression
Close_Bracket, ///< "]" ends index section of index expression
};

/// Type of token
Expand Down
9 changes: 0 additions & 9 deletions musique/parser/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ Ast Ast::sequence(std::vector<Ast> expressions)
return ast;
}

Ast Ast::block(File_Range file, Ast seq)
{
Ast ast;
ast.type = Type::Block;
ast.file = file;
ast.arguments.push_back(std::move(seq));
return ast;
}

Ast Ast::lambda(File_Range file, Ast body, std::vector<Ast> parameters)
{
Ast ast;
Expand Down
3 changes: 1 addition & 2 deletions musique/parser/ast.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ struct Ast
{
Unary, ///< Unary operator like -foo or +xd
Binary, ///< Binary operator application like `1 + 2`
Block, ///< Block expressions like `[42; hello]`
Lambda, ///< Block expression beeing functions like `[i|i+1]`
Lambda, ///< Block expression beeing functions like `(i|i+1)`
Call, ///< Function call application like `print 42`
Literal, ///< Compile time known constant like `c` or `1`
Sequence, ///< Several expressions sequences like `42`, `42; 32`
Expand Down
Loading

0 comments on commit 2f87e63

Please sign in to comment.