Skip to content

Commit

Permalink
continued with lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
t-weber committed Jul 15, 2020
1 parent 1556539 commit e7dd315
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
64 changes: 57 additions & 7 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,41 @@

%{
#include <string>
#include <sstream>
#include <iomanip>

#include "parser.h"
%}


white [ \t\r]
num [0-9]
real {num}+(\.{num}*)?
int {num}+

num_bin [01]
num_oct [0-7]
num_dec [0-9]
num_hex [0-9a-fA-F]

int_bin "0b"{num_bin}+
int_dec {num_dec}+
int_hex "0x"{num_hex}+

real {num_dec}+(\.{num_dec}*)?
realeng {real}([Ee][+-]?{num_dec}+)?

ident [A-Za-z_]+[A-Za-z0-9_]*


%%


"\n" { IncCurLine(); }
{white}+ /* whitespace */
"#".* /* comment */

";" { return yytext[0]; }
"+"|"-" { return yytext[0]; }
"*"|"/"|"%" { return yytext[0]; }
"^"|"'" { return yytext[0]; }
"^"|"'" { return yytext[0]; }
"("|")" { return yytext[0]; }
"{"|"}" { return yytext[0]; }
"["|"]" { return yytext[0]; }
Expand Down Expand Up @@ -74,18 +88,54 @@ ident [A-Za-z_]+[A-Za-z0-9_]*
"loop" { return yy::Parser::make_LOOP(); }
"do" { return yy::Parser::make_DO(); }

{int} { return yy::Parser::make_INT(std::stol(yytext)); }
{real} { return yy::Parser::make_REAL(std::stod(yytext)); }
{ident} { return yy::Parser::make_IDENT(yytext); }

{int_bin} {
long dec = 0;

std::size_t pos = 0;
for(const char* pc=yytext+yyleng-1; pc>=yytext+2; --pc)
{
if(*pc != '0')
dec += (1<<pos);
++pos;
}

return yy::Parser::make_INT(dec);
}

{int_dec} {
return yy::Parser::make_INT(std::stol(yytext));
}

{int_hex} {
long l = 0;
std::istringstream{yytext} >> std::hex >> l;
return yy::Parser::make_INT(l);
}

{realeng} {
return yy::Parser::make_REAL(std::stod(yytext));
}


{ident} {
return yy::Parser::make_IDENT(yytext);
}


"\""[^\"]*"\"" {
// string
std::string str{yytext+1, yytext+yyleng-1};
return yy::Parser::make_STRING(str); }


. {
std::string err = "Unknown token: \"";
err += yytext;
err += "\".";

LexerError(err.c_str());
}


%%
13 changes: 11 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "semantics.h"

#include <fstream>
#include <locale>
#include <boost/program_options.hpp>
namespace args = boost::program_options;

Expand Down Expand Up @@ -59,6 +60,14 @@ int main(int argc, char** argv)
{
try
{
std::ios_base::sync_with_stdio(0);
std::locale loc{};
std::locale::global(loc);
//std::cout << "Locale: " << loc.name() << "." << std::endl;

std::cout << "Matrix expression compiler version 0.2"
<< " by Tobias Weber <[email protected]>, 2020." << std::endl;

// llvm toolchain
std::string tool_opt = "opt";
std::string tool_bc = "llvm-as";
Expand All @@ -85,8 +94,8 @@ int main(int argc, char** argv)
("out,o", args::value(&outprog), "compiled program output")
("optimise,O", args::bool_switch(&optimise), "optimise program")
("interpret,i", args::bool_switch(&interpret), "directly run program in interpreter")
("symbols,s", args::bool_switch(&show_symbols), "print symbol table")
("ast,a", args::bool_switch(&show_ast), "print syntax tree")
("symbols,s", args::bool_switch(&show_symbols), "output symbol table")
("ast,a", args::bool_switch(&show_ast), "output syntax tree")
("program", args::value<decltype(vecProgs)>(&vecProgs), "input program to compile");

args::positional_options_description posarg_descr;
Expand Down
2 changes: 1 addition & 1 deletion test/cryst.prog
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func mat 3 3 A_matrix(scalar a, scalar b, scalar c, scalar alpha, scalar beta, s

func start()
{
set_eps(0.00001);
set_eps(1e-5);
#putstr("eps = " + get_eps());

scalar a = getflt("a = ");
Expand Down

0 comments on commit e7dd315

Please sign in to comment.