Andrea Gussoni 8 жил өмнө
parent
commit
02bde6a3bd
4 өөрчлөгдсөн 18 нэмэгдсэн , 22 устгасан
  1. 13 18
      source/Ast.cpp
  2. 4 1
      source/Ast.h
  3. 0 3
      source/Main.cpp
  4. 1 0
      source/Parser.cpp

+ 13 - 18
source/Ast.cpp

@@ -1,21 +1,14 @@
 #include "Ast.h"
+#include "Parser.h"
 
-using namespace ast;
-
+namespace ast{
 // attributes added from chapter 3 of the tutorial
-std::unique_ptr<Module> AstObjects::TheModule = make_unique<Module>("my cool jit", getGlobalContext());;
+std::unique_ptr<Module> AstObjects::TheModule = std::make_unique<Module>("my cool jit", getGlobalContext());
 IRBuilder<> AstObjects::Builder(getGlobalContext());
-std::map<std::string, Value *> AstObjects::NamedValues;
-
-// error method from chapter 3
-// HACK: duplicated function already present in example
-std::unique_ptr<ExprAST> Error_Ast(const char *Str) {
-  fprintf(stderr, "Error: %s\n", Str);
-  return nullptr;
-}
+std::map<std::string, Value *> AstObjects::NamedValues{};
 
 Value *ErrorV(const char *Str) {
-  Error_Ast(Str);
+  parser::Error(Str);
   return nullptr;
 }
 
@@ -75,13 +68,10 @@ Value *CallExprAST::codegen() {
 
 Function *PrototypeAST::codegen() {
   // Make the function type:  double(double,double) etc.
-  std::vector<Type *> Doubles(Args.size(),
-                              Type::getDoubleTy(getGlobalContext()));
-  FunctionType *FT =
-      FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
+  std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(getGlobalContext()));
+  FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
 
-  Function *F =
-      Function::Create(FT, Function::ExternalLinkage, Name, AstObjects::TheModule.get());
+  Function *F = Function::Create(FT, Function::ExternalLinkage, Name, AstObjects::TheModule.get());
 
   // Set names for all arguments.
   unsigned Idx = 0;
@@ -91,7 +81,10 @@ Function *PrototypeAST::codegen() {
   return F;
 }
 
+
+
 Function *FunctionAST::codegen() {
+
   // First, check for an existing function from a previous 'extern' declaration.
   Function *TheFunction = AstObjects::TheModule->getFunction(Proto->getName());
 
@@ -127,3 +120,5 @@ Function *FunctionAST::codegen() {
   TheFunction->eraseFromParent();
   return nullptr;
 }
+
+}

+ 4 - 1
source/Ast.h

@@ -93,7 +93,10 @@ class FunctionAST {
 
 public:
   FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body) : Proto(std::move(Proto)), Body(std::move(Body)) {}
-    Function *codegen();
+  Function *codegen();
 };
+
+llvm::Value *ErrorV(const char *Str);
+
 }
 #endif

+ 0 - 3
source/Main.cpp

@@ -36,12 +36,9 @@ static
 //===----------------------------------------------------------------------===//
 
 int main() {
-
-
   // Prime the first token.
   fprintf(stderr, "ready> ");
   getNextToken();
-
   // Run the main "interpreter loop" now.
   MainLoop();
 

+ 1 - 0
source/Parser.cpp

@@ -261,6 +261,7 @@ void MainLoop() {
   BinopPrecedence['+'] = 20;
   BinopPrecedence['-'] = 20;
   BinopPrecedence['*'] = 40; // highest.
+
   while (1) {
     fprintf(stderr, "ready> ");
     switch (CurTok) {