Fix else ifs

This commit is contained in:
Danila Fedorin 2019-05-27 21:51:39 -07:00
parent 2ef9dd73cc
commit 626fd1629b
4 changed files with 25 additions and 23 deletions

View File

@ -4,13 +4,13 @@ parser.cpp parser.hpp: parser.y
bison -d -o parser.cpp parser.y bison -d -o parser.cpp parser.y
tree.o: tree.cpp tree.o: tree.cpp
g++ -g -c -o tree.o tree.cpp g++ -std=c++11 -g -c -o tree.o tree.cpp
scanner.cpp: scanner.l scanner.cpp: scanner.l
flex -o scanner.cpp scanner.l flex -o scanner.cpp scanner.l
parse: main.cpp parser.cpp scanner.cpp tree.o parse: main.cpp parser.cpp scanner.cpp tree.o
g++ -g main.cpp parser.cpp scanner.cpp tree.o -o parse g++ -std=c++11 -g main.cpp parser.cpp scanner.cpp tree.o -o parse
clean: clean:
rm -f parse scanner.cpp parser.cpp parser.hpp tree.o rm -f parse scanner.cpp parser.cpp parser.hpp tree.o

View File

@ -134,17 +134,18 @@ condition
; ;
if_statement if_statement
: IF condition COLON NEWLINE block elif_blocks else_block { $$ = new stmt_if($2, $5, $7); delete $6; } : IF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); }
; ;
elif_blocks elif_blocks
: %empty { $$ = nullptr; } : %empty { $$ = nullptr; }
| elif_blocks ELIF condition COLON NEWLINE block { $$ = new stmt_if($3, $6); delete $6; } | else_block { $$ = $1; }
| ELIF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); }
; ;
else_block else_block
: %empty { $$ = nullptr; } : ELSE COLON NEWLINE block { $$ = $4; }
| ELSE COLON NEWLINE block { $$ = $4; } ;
while_statement while_statement

View File

@ -20,29 +20,29 @@ template <>
std::string label<binop>(binop op) { std::string label<binop>(binop op) {
switch(op) { switch(op) {
case binop::plus: case binop::plus:
return "+"; return "PLUS";
case binop::minus: case binop::minus:
return "-"; return "MINUS";
case binop::times: case binop::times:
return "*"; return "TIMES";
case binop::divide: case binop::divide:
return "/"; return "DIVIDEBY";
case binop::land: case binop::land:
return "&&"; return "AND";
case binop::lor: case binop::lor:
return "||"; return "OR";
case binop::eq: case binop::eq:
return "=="; return "EQ";
case binop::neq: case binop::neq:
return "!="; return "NEQ";
case binop::lt: case binop::lt:
return "<"; return "LT";
case binop::lte: case binop::lte:
return "<="; return "LTE";
case binop::gt: case binop::gt:
return ">"; return "GT";
case binop::gte: case binop::gte:
return ">="; return "GTE";
default: default:
return ""; return "";
} }
@ -59,20 +59,20 @@ std::string label<unop>(unop op) {
} }
template <typename T> template <typename T>
std::string wrap_label(T v) { std::string wrap_label(T v, bool box = false) {
return std::string(" [label=\"") + label(v) + "\"]"; return std::string(" [label=\"") + label(v) + "\" " + (box ? "shape=\"box\"" : "") + "]";
} }
void expr_id::print_dot(dot& into, std::string prefix) { void expr_id::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(id)); into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true));
} }
void expr_int::print_dot(dot& into, std::string prefix) { void expr_int::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(val)); into.push_back(prefix + wrap_label(std::string("integer: ") + label(val), true));
} }
void expr_float::print_dot(dot& into, std::string prefix) { void expr_float::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(val)); into.push_back(prefix + wrap_label(std::string("float: ") + label(val), true));
} }
void expr_binop::print_dot(dot& into, std::string prefix) { void expr_binop::print_dot(dot& into, std::string prefix) {

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string>
typedef std::vector<std::string> dot; typedef std::vector<std::string> dot;