Skip to content
Browse files

print alone expressions

  • Loading branch information...
1 parent 67017c3 commit eeb7ad04593dc6dcd6cb166a293d0e80dfe51181 @galchinsky galchinsky committed Nov 26, 2012
Showing with 36 additions and 8 deletions.
  1. +18 −6 compiler/interactive.cpp
  2. +16 −2 compiler/parser.cpp
  3. +2 −0 compiler/parser.hpp
View
24 compiler/interactive.cpp
@@ -87,7 +87,7 @@ namespace clay {
}
}
- static void loadImports(llvm::ArrayRef<ImportPtr> imports)
+ static void loadImports(llvm::ArrayRef<ImportPtr> imports)
{
for (size_t i = 0; i < imports.size(); ++i) {
module->imports.push_back(imports[i]);
@@ -100,7 +100,7 @@ namespace clay {
}
}
- static void jitTopLevel(llvm::ArrayRef<TopLevelItemPtr> toplevels)
+ static void jitTopLevel(llvm::ArrayRef<TopLevelItemPtr> toplevels)
{
if (toplevels.empty()) {
return;
@@ -113,7 +113,7 @@ namespace clay {
addGlobals(module, toplevels);
}
- static void jitStatements(llvm::ArrayRef<StatementPtr> statements)
+ static void jitStatements(llvm::ArrayRef<StatementPtr> statements)
{
if (statements.empty()) {
return;
@@ -160,6 +160,14 @@ namespace clay {
engine->runFunction(entryProc->llvmFunc, std::vector<llvm::GenericValue>());
}
+ static void jitAndPrintExpr(ExprPtr expr) {
+ //expr -> println(expr);
+ NameRefPtr println = new NameRef(Identifier::get("println"));
+ ExprPtr call = new Call(println.ptr(), new ExprList(expr));
+ ExprStatementPtr callStmt = new ExprStatement(call);
+ jitStatements(vector<StatementPtr>(1, callStmt.ptr()));
+ }
+
static void interactiveLoop()
{
setjmp(recovery);
@@ -176,9 +184,13 @@ namespace clay {
SourcePtr source = new Source(line, 0);
try {
ReplItem x = parseInteractive(source, 0, source->size());
- loadImports(x.imports);
- jitTopLevel(x.toplevels);
- jitStatements(x.stmts);
+ if (x.isExprSet) {
+ jitAndPrintExpr(x.expr);
+ } else {
+ loadImports(x.imports);
+ jitTopLevel(x.toplevels);
+ jitStatements(x.stmts);
+ }
}
catch (CompilerError) {
continue;
View
18 compiler/parser.cpp
@@ -9,6 +9,7 @@ map<llvm::StringRef, IdentifierPtr> Identifier::freeIdentifiers;
static vector<Token> *tokens;
static int position;
static int maxPosition;
+static int sourceSize;
static bool parserOptionKeepDocumentation = false;
static AddTokensCallback addTokens = NULL;
@@ -17,7 +18,7 @@ void setAddTokens(AddTokensCallback f) {
addTokens = f;
}
-bool inRepl = false;
+static bool inRepl = false;
static bool next(Token *&x) {
if (position == (int)tokens->size()) {
@@ -3111,12 +3112,21 @@ static bool module(llvm::StringRef moduleName, ModulePtr &x) {
//
static bool replItems(ReplItem& x, bool = false) {
+ inRepl = false;
+ int p = save();
+ if (expression(x.expr) && position == sourceSize) {
+ x.isExprSet = true;
+ return true;
+ }
+ restore(p);
+
+ inRepl = true;
x.toplevels.clear();
x.imports.clear();
x.stmts.clear();
ImportPtr importItem;
StatementPtr stmtItem;
- inRepl = true;
+
while (true) {
if (position == (int)tokens->size()) {
break;
@@ -3144,6 +3154,9 @@ static bool replItems(ReplItem& x, bool = false) {
x.stmts.push_back(stmtItem);
}
}
+
+ x.isExprSet = false;
+
return true;
}
@@ -3160,6 +3173,7 @@ void applyParser(SourcePtr source, int offset, int length, Parser parser, Parser
tokens = &t;
position = maxPosition = 0;
+ sourceSize = t.size();
if (!parser(node, parserParam) || (position < (int)t.size())) {
Location location;
View
2 compiler/parser.hpp
@@ -13,6 +13,8 @@ enum ParserFlags
};
struct ReplItem {
+ bool isExprSet;
+ ExprPtr expr;
vector<TopLevelItemPtr> toplevels;
vector<ImportPtr> imports;
vector<StatementPtr> stmts;

0 comments on commit eeb7ad0

Please sign in to comment.
Something went wrong with that request. Please try again.