Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add 2 repl commands: modules, globals

```
clay>var x = 5; var y = 7;
clay>:globals
x : Int32 : 5
y : Int32 : 7
clay>x = x + y;

clay>:globals
x : Int32 : 12
y : Int32 : 7
```
  • Loading branch information...
commit fc28417d46b45dd40de7ee522e6b1bc99ebead15 1 parent eeb7ad0
@galchinsky galchinsky authored
Showing with 83 additions and 32 deletions.
  1. +82 −29 compiler/interactive.cpp
  2. +1 −3 compiler/parser.cpp
View
111 compiler/interactive.cpp
@@ -11,7 +11,7 @@
namespace clay {
- using std::ostringstream;
+ typedef llvm::SmallString<16U> Str;
const char* replAnonymousFunctionName = "__replAnonymousFunction__";
@@ -22,6 +22,8 @@ namespace clay {
static bool printAST = false;
+ static void eval(llvm::StringRef code);
+
string newFunctionName()
{
static int funNum = 0;
@@ -51,30 +53,75 @@ namespace clay {
tokenize(source, 0, line.length(), tokens);
return tokens;
}
+
+ static void cmdGlobals(const vector<Token>& tokens) {
+ ModulePtr m;
+ if (tokens.size() == 1) {
+ m = module;
+ } else if (tokens.size() == 2) {
+ m = globalModules[tokens[1].str];
+ } else {
+ llvm::errs() << ":globals [module name]\n";
+ return;
+ }
+
+ string buf;
+ llvm::raw_string_ostream code(buf);
+
+ llvm::StringMap<ObjectPtr>::const_iterator g = m->globals.begin();
+ for (; g != m->globals.end(); ++g) {
+ if (g->second->objKind == GLOBAL_VARIABLE) {
+ IdentifierPtr name = ((GlobalVariable*)g->second.ptr())->name;
+ code << "println(\"" << name->str << " : \", "
+ << "Type(" << name->str << "), \" : \", "
+ << name->str <<");\n";
+ }
+ }
+ code.flush();
+ eval(buf);
+ }
+
+ static void cmdModules(const vector<Token>& tokens) {
+ if (tokens.size() != 1) {
+ llvm::errs() << "Warning: command parameters are ignored\n";
+ }
+ llvm::StringMap<ModulePtr>::const_iterator iter = globalModules.begin();
+ for (; iter != globalModules.end(); ++iter) {
+ llvm::errs() << iter->getKey() << "\n";
+ }
+ }
+
+ static void cmdPrint(const vector<Token>& tokens) {
+ for (size_t i = 1; i < tokens.size(); ++i) {
+ if (tokens[i].tokenKind == T_IDENTIFIER) {
+ Str identifier = tokens[i].str;
+ llvm::StringMap<ImportSet>::const_iterator iter = module->allSymbols.find(identifier);
+ if (iter == module->allSymbols.end()) {
+ llvm::errs() << "Can't find identifier " << identifier.c_str();
+ } else {
+ for (size_t i = 0; i < iter->second.size(); ++i) {
+ llvm::errs() << iter->second[i] << "\n";
+ }
+ }
+ }
+ }
+ }
static void replCommand(string const& line)
{
- typedef llvm::SmallString<16U> Str;
SourcePtr source = new Source(line, 0);
vector<Token> tokens;
+ //TODO: don't use compiler's tokenizer
tokenize(source, 0, line.length(), tokens);
Str cmd = tokens[0].str;
if (cmd == "q") {
exit(0);
+ } else if (cmd == "globals") {
+ cmdGlobals(tokens);
+ } else if (cmd == "modules") {
+ cmdModules(tokens);
} else if (cmd == "print") {
- for (size_t i = 1; i < tokens.size(); ++i) {
- if (tokens[i].tokenKind == T_IDENTIFIER) {
- Str identifier = tokens[i].str;
- llvm::StringMap<ImportSet>::const_iterator iter = module->allSymbols.find(identifier);
- if (iter == module->allSymbols.end()) {
- llvm::errs() << "Can't find identifier " << identifier.c_str();
- } else {
- for (size_t i = 0; i < iter->second.size(); ++i) {
- llvm::errs() << iter->second[i] << "\n";
- }
- }
- }
- }
+ cmdPrint(tokens);
} else if (cmd == "ast_on") {
printAST = true;
} else if (cmd == "ast_off") {
@@ -168,6 +215,23 @@ namespace clay {
jitStatements(vector<StatementPtr>(1, callStmt.ptr()));
}
+ static void eval(llvm::StringRef line) {
+ SourcePtr source = new Source(line, 0);
+ try {
+ ReplItem x = parseInteractive(source, 0, source->size());
+ if (x.isExprSet) {
+ jitAndPrintExpr(x.expr);
+ } else {
+ loadImports(x.imports);
+ jitTopLevel(x.toplevels);
+ jitStatements(x.stmts);
+ }
+ }
+ catch (CompilerError) {
+ return;
+ }
+ }
+
static void interactiveLoop()
{
setjmp(recovery);
@@ -181,20 +245,7 @@ namespace clay {
if (line[0] == ':') {
replCommand(line.substr(1, line.size() - 1));
} else {
- SourcePtr source = new Source(line, 0);
- try {
- ReplItem x = parseInteractive(source, 0, source->size());
- if (x.isExprSet) {
- jitAndPrintExpr(x.expr);
- } else {
- loadImports(x.imports);
- jitTopLevel(x.toplevels);
- jitStatements(x.stmts);
- }
- }
- catch (CompilerError) {
- continue;
- }
+ eval(line);
}
}
engine->runStaticConstructorsDestructors(true);
@@ -217,6 +268,8 @@ namespace clay {
llvm::errs() << "Clay interpreter\n";
llvm::errs() << ":q to exit\n";
llvm::errs() << ":print {identifier} to print an identifier\n";
+ llvm::errs() << ":modules to list global modules\n";
+ llvm::errs() << ":globals to list globals\n";
llvm::errs() << "In multi-line mode empty line to exit\n";
llvm::EngineBuilder eb(llvmModule);
View
4 compiler/parser.cpp
@@ -9,7 +9,6 @@ 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;
@@ -3114,7 +3113,7 @@ 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) {
+ if (expression(x.expr) && position == tokens->size()) {
x.isExprSet = true;
return true;
}
@@ -3173,7 +3172,6 @@ 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;

0 comments on commit fc28417

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