summaryrefslogtreecommitdiff
path: root/ast.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ast.cpp')
-rw-r--r--ast.cpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/ast.cpp b/ast.cpp
index d1742e2..d903ad5 100644
--- a/ast.cpp
+++ b/ast.cpp
@@ -55,7 +55,7 @@ Scope::Scope(Type type,const StatementList &body,const vector<Expression> &args)
}*/
string indent(int amount){
- return string(amount*4,' ');
+ return string(amount,'\t');
}
ostream& operator<<(ostream &os,const Scope &scope){
@@ -74,14 +74,15 @@ ostream& operator<<(ostream &os,const Scope &scope){
}
os<<')';
}
+ if(scope.body.size()==0)return os<<"{}";
os<<"{\n";
os.iword(indent_level_index)++;
int level=os.iword(indent_level_index);
- os<<scope.body;
+ os<<indent(level)<<scope.body;
os.iword(indent_level_index)--;
- return os<<'\n'<<indent(level)<<'}';
+ return os<<'\n'<<indent(level-1)<<'}';
}
@@ -150,6 +151,7 @@ ostream& operator<<(ostream &os,const Expression &expr){
os<<')';
}
if(expr.type==Expression::Type::dive){
+ if(expr.args.size()==0)os<<' ';
os<<expr.scope;
}
return os;
@@ -158,10 +160,20 @@ ostream& operator<<(ostream &os,const Expression &expr){
return os<<expr.numval;
case Expression::Type::string:
- return os<<expr.strval;
+ os<<'"';
+ for(char c : expr.strval){
+ if(c=='"')os<<"\\\"";
+ else if(c=='\n')os<<"\\n";
+ else if(c=='\r')os<<"\\r";
+ else if(c=='\t')os<<"\\t";
+ else if(c>=32&&c<=126)os<<c;
+ else os<<"\\x"<<"0123456789abcdef"[(unsigned char)c/16]
+ <<"0123456789abcdef"[(unsigned char)c%16];
+ }
+ return os<<'"';
case Expression::Type::scope:
- throw runtime_error("operator<< on Expression:scope not implemented");
+ return os<<expr.scope;
case Expression::Type::cond:
return os<<"if "<<expr.args[0]<<" then "<<expr.args[1]<<" else "<<expr.args[2];