Fixed build error.

This commit is contained in:
yhirose 2015-06-03 08:16:33 -04:00
parent f9fea06e74
commit 1e4ad36f25
6 changed files with 92 additions and 92 deletions

0
language/interpreter.cc Normal file → Executable file
View File

2
language/interpreter.hpp Normal file → Executable file
View File

@ -28,7 +28,7 @@ struct Value
Value(Value&& rhs) : type(rhs.type), v(rhs.v) {}
Value& operator=(const Value&) = default;
Value& operator=(Value&&) = default;
Value& operator=(Value&& rhs) { type = rhs.type; v = rhs.v; return *this; }
bool to_bool() const {
switch (type) {

0
language/main.cc Normal file → Executable file
View File

182
language/parser.cc Normal file → Executable file
View File

@ -1,91 +1,91 @@
#include "parser.hpp"
using namespace peglib;
using namespace std;
static auto g_grammar = R"(
PROGRAM <- _ STATEMENTS
STATEMENTS <- EXPRESSION*
EXPRESSION <- ASSIGNMENT / PRIMARY
ASSIGNMENT <- IDENTIFIER '=' _ EXPRESSION
WHILE <- 'while' _ EXPRESSION BLOCK
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
FUNCTION <- 'fn' _ PARAMETERS BLOCK
PARAMETERS <- '(' _ (IDENTIFIER (',' _ IDENTIFIER)*)? ')' _
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
PRIMARY <- CONDITION (CONDITION_OPERATOR CONDITION)?
CONDITION <- TERM (TERM_OPERATOR TERM)*
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
BLOCK <- '{' _ STATEMENTS '}' _
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
TERM_OPERATOR <- < [-+] > _
FACTOR_OPERATOR <- < [*/%] > _
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
~_ <- (Space / EndOfLine / Comment)*
Space <- ' ' / '\t'
EndOfLine <- '\r\n' / '\n' / '\r'
EndOfFile <- !.
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
)";
peg& get_parser()
{
static peg parser;
static bool initialized = false;
if (!initialized) {
initialized = true;
parser.log = [&](size_t ln, size_t col, const string& msg) {
cerr << ln << ":" << col << ": " << msg << endl;
};
if (!parser.load_grammar(g_grammar)) {
throw logic_error("invalid peg grammar");
}
parser.ast({
{ peg::AstNodeType::Regular, "STATEMENTS", Statements },
{ peg::AstNodeType::Regular, "WHILE", While },
{ peg::AstNodeType::Regular, "ASSIGNMENT", Assignment },
{ peg::AstNodeType::Regular, "IF", If },
{ peg::AstNodeType::Regular, "FUNCTION", Function },
{ peg::AstNodeType::Regular, "PARAMETERS", Undefined },
{ peg::AstNodeType::Regular, "FUNCTION_CALL", FunctionCall },
{ peg::AstNodeType::Regular, "ARGUMENTS", Undefined },
{ peg::AstNodeType::Optimizable, "PRIMARY", Condition },
{ peg::AstNodeType::Optimizable, "CONDITION", BinExpresion },
{ peg::AstNodeType::Optimizable, "TERM", BinExpresion },
{ peg::AstNodeType::Token, "CONDITION_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "TERM_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "FACTOR_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "NUMBER", Number },
{ peg::AstNodeType::Token, "BOOLEAN", Boolean },
{ peg::AstNodeType::Token, "STRING", Undefined },
{ peg::AstNodeType::Token, "IDENTIFIER", Identifier },
{ peg::AstNodeType::Regular, "INTERPOLATED_STRING", InterpolatedString },
{ peg::AstNodeType::Token, "INTERPOLATED_CONTENT", Undefined },
},
Undefined);
}
return parser;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix
#include "parser.hpp"
using namespace peglib;
using namespace std;
static auto g_grammar = R"(
PROGRAM <- _ STATEMENTS
STATEMENTS <- EXPRESSION*
EXPRESSION <- ASSIGNMENT / PRIMARY
ASSIGNMENT <- IDENTIFIER '=' _ EXPRESSION
WHILE <- 'while' _ EXPRESSION BLOCK
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
FUNCTION <- 'fn' _ PARAMETERS BLOCK
PARAMETERS <- '(' _ (IDENTIFIER (',' _ IDENTIFIER)*)? ')' _
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
PRIMARY <- CONDITION (CONDITION_OPERATOR CONDITION)?
CONDITION <- TERM (TERM_OPERATOR TERM)*
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
BLOCK <- '{' _ STATEMENTS '}' _
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
TERM_OPERATOR <- < [-+] > _
FACTOR_OPERATOR <- < [*/%] > _
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
~_ <- (Space / EndOfLine / Comment)*
Space <- ' ' / '\t'
EndOfLine <- '\r\n' / '\n' / '\r'
EndOfFile <- !.
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
)";
peg& get_parser()
{
static peg parser;
static bool initialized = false;
if (!initialized) {
initialized = true;
parser.log = [&](size_t ln, size_t col, const string& msg) {
cerr << ln << ":" << col << ": " << msg << endl;
};
if (!parser.load_grammar(g_grammar)) {
throw logic_error("invalid peg grammar");
}
parser.ast({
{ peg::AstNodeType::Regular, "STATEMENTS", Statements },
{ peg::AstNodeType::Regular, "WHILE", While },
{ peg::AstNodeType::Regular, "ASSIGNMENT", Assignment },
{ peg::AstNodeType::Regular, "IF", If },
{ peg::AstNodeType::Regular, "FUNCTION", Function },
{ peg::AstNodeType::Regular, "PARAMETERS", Undefined },
{ peg::AstNodeType::Regular, "FUNCTION_CALL", FunctionCall },
{ peg::AstNodeType::Regular, "ARGUMENTS", Undefined },
{ peg::AstNodeType::Optimizable, "PRIMARY", Condition },
{ peg::AstNodeType::Optimizable, "CONDITION", BinExpresion },
{ peg::AstNodeType::Optimizable, "TERM", BinExpresion },
{ peg::AstNodeType::Token, "CONDITION_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "TERM_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "FACTOR_OPERATOR", Undefined },
{ peg::AstNodeType::Token, "NUMBER", Number },
{ peg::AstNodeType::Token, "BOOLEAN", Boolean },
{ peg::AstNodeType::Token, "STRING", Undefined },
{ peg::AstNodeType::Token, "IDENTIFIER", Identifier },
{ peg::AstNodeType::Regular, "INTERPOLATED_STRING", InterpolatedString },
{ peg::AstNodeType::Token, "INTERPOLATED_CONTENT", Undefined },
},
Undefined);
}
return parser;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

0
language/repl.cc Normal file → Executable file
View File

0
language/repl.hpp Normal file → Executable file
View File