Permalink
Browse files

Merge remote-tracking branch 'jckarter/master' into parser

  • Loading branch information...
2 parents f14231c + 62a49d0 commit 97cfecab5441ad718babf5b33c04aa9b03f9b83a agemogolk committed Dec 3, 2012
Showing with 2,545 additions and 2,283 deletions.
  1. +1 −1 compiler/analyzer.cpp
  2. +3 −3 compiler/clay.cpp
  3. +0 −24 compiler/clay.hpp
  4. +1 −0 compiler/claydoc.cpp
  5. +1 −1 compiler/codegen.cpp
  6. +1 −1 compiler/desugar.cpp
  7. +133 −31 compiler/interactive.cpp
  8. +13 −0 compiler/invoketables.cpp
  9. +1 −0 compiler/invoketables.hpp
  10. +18 −9 compiler/loader.cpp
  11. +2 −2 compiler/loader.hpp
  12. +45 −17 compiler/parser.cpp
  13. +37 −0 compiler/parser.hpp
  14. +2 −0 lib-clay/data/strings/tempcstrings/tempcstrings.clay
  15. +496 −475 lib-clay/libc/generated/generated.freebsd.32.clay
  16. +464 −456 lib-clay/libc/generated/generated.freebsd.64.clay
  17. +2 −1 lib-clay/opengl/nolookup/nolookup.clay
  18. +2 −1 lib-clay/opengl/opengl.clay
  19. +2 −0 lib-clay/prelude/repl/repl.clay
  20. +680 −651 lib-clay/unix/generated/generated.freebsd.32.clay
  21. +631 −610 lib-clay/unix/generated/generated.freebsd.64.clay
  22. +1 −0 test/lib-clay/atomics/casValue/buildflags.freebsd.txt
  23. +1 −0 test/lib-clay/atomics/normallyUnalignedType/buildflags.freebsd.txt
  24. +1 −0 test/lib-clay/atomics/normallyUnalignedType2/buildflags.freebsd.txt
  25. +1 −0 test/lib-clay/atomics/rmw/buildflags.freebsd.txt
  26. +1 −0 test/lib-clay/threads/condvars/buildflags.freebsd.txt
  27. +1 −0 test/lib-clay/threads/core/simple/buildflags.freebsd.txt
  28. +1 −0 test/lib-clay/threads/future/buildflags.freebsd.txt
  29. +1 −0 test/lib-clay/threads/locks/mutex/buildflags.freebsd.txt
  30. +1 −0 test/lib-clay/threads/locks/spinlock/buildflags.freebsd.txt
  31. +1 −0 test/lib-clay/threads/threadlocal/buildflags.freebsd.txt
View
@@ -2600,7 +2600,7 @@ static StatementAnalysis analyzeStatement(StatementPtr stmt, EnvPtr env, Analysi
return SA_RECURSIVE;
}
- MultiPValue *cond = analyzeExpr(x->condition, env2).ptr();
+ MultiPValuePtr cond = analyzeExpr(x->condition, env2);
if (cond->values.size() != 1) {
arityError(1, cond->values.size());
View
@@ -1096,11 +1096,11 @@ int main2(int argc, char **argv, char const* const* envp) {
vector<string> sourceFiles;
if (!clayScript.empty()) {
clayScriptSource = clayScriptImports + "main() {\n" + clayScript + "}";
- m = loadProgramSource("-e", clayScriptSource, verbose);
+ m = loadProgramSource("-e", clayScriptSource, verbose, repl);
} else if (generateDeps)
- m = loadProgram(clayFile, &sourceFiles, verbose);
+ m = loadProgram(clayFile, &sourceFiles, verbose, repl);
else
- m = loadProgram(clayFile, NULL, verbose);
+ m = loadProgram(clayFile, NULL, verbose, repl);
loadTimer.stop();
compileTimer.start();
View
@@ -2481,30 +2481,6 @@ struct MultiPValue : public Object {
//
-// parser module
-//
-
-
-enum ParserFlags
-{
- NoParserFlags = 0,
- ParserKeepDocumentation = 1
-};
-
-ModulePtr parse(llvm::StringRef moduleName, SourcePtr source, ParserFlags flags = NoParserFlags);
-ExprPtr parseExpr(SourcePtr source, int offset, int length);
-ExprListPtr parseExprList(SourcePtr source, int offset, int length);
-void parseStatements(SourcePtr source, int offset, int length,
- vector<StatementPtr> &statements);
-void parseTopLevelItems(SourcePtr source, int offset, int length,
- vector<TopLevelItemPtr> &topLevels, Module *);
-void parseInteractive(SourcePtr source, int offset, int length,
- vector<TopLevelItemPtr>& toplevels,
- vector<ImportPtr>& imports,
- vector<StatementPtr>& stmts);
-
-
-//
// printer module
//
View
@@ -1,4 +1,5 @@
#include "claydoc.hpp"
+#include "parser.hpp"
#include <sstream>
#include <string>
View
@@ -11,7 +11,7 @@
#include "literals.hpp"
#include "desugar.hpp"
#include "constructors.hpp"
-
+#include "parser.hpp"
#pragma clang diagnostic ignored "-Wcovered-switch-default"
View
@@ -3,7 +3,7 @@
#include "evaluator.hpp"
#include "analyzer.hpp"
#include "desugar.hpp"
-
+#include "parser.hpp"
namespace clay {
View
@@ -1,16 +1,18 @@
#include "clay.hpp"
#include "lexer.hpp"
+#include "parser.hpp"
#include "codegen.hpp"
#include "error.hpp"
#include "loader.hpp"
+#include "invoketables.hpp"
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
namespace clay {
- using std::ostringstream;
+ typedef llvm::SmallString<16U> Str;
const char* replAnonymousFunctionName = "__replAnonymousFunction__";
@@ -21,6 +23,8 @@ namespace clay {
static bool printAST = false;
+ static void eval(llvm::StringRef code);
+
string newFunctionName()
{
static int funNum = 0;
@@ -41,29 +45,109 @@ namespace clay {
return s.substr(i, s.length());
}
+ static vector<Token> addTokens() {
+ char buf[255];
+ string line = fgets(buf, 255, stdin);
+ line = stripSpaces(line);
+ SourcePtr source = new Source(line, 0);
+ vector<Token> tokens;
+ 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 cmdOverloads(const vector<Token>& tokens) {
+ for (size_t i = 1; i < tokens.size(); ++i) {
+ if (tokens[i].tokenKind == T_IDENTIFIER) {
+ Str identStr = tokens[i].str;
+
+ ObjectPtr obj = lookupPrivate(module, Identifier::get(identStr));
+ if (obj == NULL || obj->objKind != PROCEDURE) {
+ llvm::errs() << identStr << " is not a procedure name\n";
+ continue;
+ }
+
+ vector<InvokeSet*> sets = lookupInvokeSets(obj.ptr());
+ for (size_t k = 0; k < sets.size(); ++k) {
+ llvm::errs() << " ";
+ for (size_t l = 0; l < sets[k]->argsKey.size(); ++l) {
+ llvm::errs() << sets[k]->argsKey[l] << " : ";
+ }
+ llvm::errs() << "\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 == "overloads") {
+ cmdOverloads(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") {
@@ -76,7 +160,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]);
@@ -89,7 +173,7 @@ namespace clay {
}
}
- static void jitTopLevel(llvm::ArrayRef<TopLevelItemPtr> toplevels)
+ static void jitTopLevel(llvm::ArrayRef<TopLevelItemPtr> toplevels)
{
if (toplevels.empty()) {
return;
@@ -102,7 +186,7 @@ namespace clay {
addGlobals(module, toplevels);
}
- static void jitStatements(llvm::ArrayRef<StatementPtr> statements)
+ static void jitStatements(llvm::ArrayRef<StatementPtr> statements)
{
if (statements.empty()) {
return;
@@ -149,6 +233,31 @@ 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 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);
@@ -162,19 +271,7 @@ namespace clay {
if (line[0] == ':') {
replCommand(line.substr(1, line.size() - 1));
} else {
- SourcePtr source = new Source(line, 0);
- vector<StatementPtr> stmts;
- vector<TopLevelItemPtr> toplevels;
- vector<ImportPtr> imports;
- try {
- parseInteractive(source, 0, source->size(), toplevels, imports, stmts);
- loadImports(imports);
- jitTopLevel(toplevels);
- jitStatements(stmts);
- }
- catch (CompilerError) {
- continue;
- }
+ eval(line);
}
}
engine->runStaticConstructorsDestructors(true);
@@ -197,6 +294,9 @@ 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);
llvm::TargetOptions targetOptions;
@@ -205,6 +305,8 @@ namespace clay {
engine = eb.create();
engine->runStaticConstructorsDestructors(false);
+ setAddTokens(&addTokens);
+
interactiveLoop();
}
View
@@ -171,6 +171,19 @@ InvokeSet* lookupInvokeSet(ObjectPtr callable,
return invokeSet;
}
+vector<InvokeSet*> lookupInvokeSets(ObjectPtr callable) {
+ assert(invokeTablesInitialized);
+ vector<InvokeSet*> r;
+ for (size_t i = 0; i < invokeTable.size(); ++i) {
+ for (size_t j = 0; j < invokeTable[i].size(); ++j) {
+ InvokeSet* set = invokeTable[i][j];
+ if (objectEquals(set->callable, callable)) {
+ r.push_back(set);
+ }
+ }
+ }
+ return r;
+}
//
@@ -105,6 +105,7 @@ struct MatchFailureError {
InvokeSet *lookupInvokeSet(ObjectPtr callable,
llvm::ArrayRef<TypePtr> argsKey);
+vector<InvokeSet*> lookupInvokeSets(ObjectPtr callable);
InvokeEntry* lookupInvokeEntry(ObjectPtr callable,
llvm::ArrayRef<TypePtr> argsKey,
llvm::ArrayRef<ValueTempness> argsTempness,
Oops, something went wrong.

0 comments on commit 97cfeca

Please sign in to comment.