Explorar o código

fix in the lexer

Andrea Gussoni %!s(int64=8) %!d(string=hai) anos
pai
achega
13a3541e39
Modificáronse 6 ficheiros con 32 adicións e 24 borrados
  1. 5 4
      source/Ast.cpp
  2. 4 3
      source/Ast.h
  3. 9 5
      source/Lexer.cpp
  4. 7 4
      source/Lexer.h
  5. 5 6
      source/Parser.cpp
  6. 2 2
      source/Parser.h

+ 5 - 4
source/Ast.cpp

@@ -5,7 +5,7 @@ namespace ast{
 // attributes added from chapter 3 of the tutorial
 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{};
+std::map<std::string, Value *> AstObjects::NamedValues;
 
 Value *ErrorV(const char *Str) {
   parser::Error(Str);
@@ -81,7 +81,7 @@ Function *PrototypeAST::codegen() {
   return F;
 }
 
-
+const std::string &PrototypeAST::getName() const { return Name; }
 
 Function *FunctionAST::codegen() {
 
@@ -103,8 +103,9 @@ Function *FunctionAST::codegen() {
 
   // Record the function arguments in the NamedValues map.
   AstObjects::NamedValues.clear();
-  for (auto &Arg : TheFunction->args())
-  AstObjects::NamedValues[Arg.getName()] = &Arg;
+  for (auto &Arg : TheFunction->args()) {
+    AstObjects::NamedValues[Arg.getName()] = &Arg;
+  }
 
   if (Value *RetVal = Body->codegen()) {
     // Finish off the function.

+ 4 - 3
source/Ast.h

@@ -1,5 +1,5 @@
-#ifndef AST_H
-#define AST_H
+#ifndef _AST_H
+#define _AST_H
 
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/IRBuilder.h"
@@ -83,7 +83,7 @@ class PrototypeAST {
 public:
   PrototypeAST(const std::string &Name, std::vector<std::string> Args) : Name(Name), Args(std::move(Args)) {}
   Function *codegen();
-  const std::string &getName() const { return Name; }
+  const std::string &getName() const;
 };
 
 /// FunctionAST - This class represents a function definition itself.
@@ -99,4 +99,5 @@ public:
 llvm::Value *ErrorV(const char *Str);
 
 }
+
 #endif

+ 9 - 5
source/Lexer.cpp

@@ -2,6 +2,10 @@
 
 /// gettok - Return the next token from standard input.
 namespace lexer{
+
+std::string LexerObjects::IdentifierStr; // Filled in if tok_identifier
+double LexerObjects::NumVal;
+
 int gettok() {
   static int LastChar = ' ';
 
@@ -10,13 +14,13 @@ int gettok() {
   LastChar = getchar();
 
   if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
-    IdentifierStr = LastChar;
+    LexerObjects::IdentifierStr = LastChar;
     while (isalnum((LastChar = getchar())))
-    IdentifierStr += LastChar;
+    LexerObjects::IdentifierStr += LastChar;
 
-    if (IdentifierStr == "def")
+    if (LexerObjects::IdentifierStr == "def")
     return tok_def;
-    if (IdentifierStr == "extern")
+    if (LexerObjects::IdentifierStr == "extern")
     return tok_extern;
     return tok_identifier;
   }
@@ -28,7 +32,7 @@ int gettok() {
       LastChar = getchar();
     } while (isdigit(LastChar) || LastChar == '.');
 
-    NumVal = strtod(NumStr.c_str(), nullptr);
+    LexerObjects::NumVal = strtod(NumStr.c_str(), nullptr);
     return tok_number;
   }
 

+ 7 - 4
source/Lexer.h

@@ -1,5 +1,5 @@
-#ifndef LEXER_H
-#define LEXER_H
+#ifndef _LEXER_H
+#define _LEXER_H
 
 #include <cctype>
 #include <cstdio>
@@ -23,8 +23,11 @@ enum Token {
   tok_number = -5
 };
 
-static std::string IdentifierStr; // Filled in if tok_identifier
-static double NumVal;
+struct LexerObjects {
+public:
+  static std::string IdentifierStr; // Filled in if tok_identifier
+  static double NumVal;
+};
 
 int gettok();
 }

+ 5 - 6
source/Parser.cpp

@@ -41,7 +41,7 @@ static std::unique_ptr<ExprAST> ParseExpression();
 
 /// numberexpr ::= number
 static std::unique_ptr<ExprAST> ParseNumberExpr() {
-  auto Result = llvm::make_unique<NumberExprAST>(NumVal);
+  auto Result = llvm::make_unique<NumberExprAST>(LexerObjects::NumVal);
   getNextToken(); // consume the number
   return std::move(Result);
 }
@@ -63,7 +63,7 @@ static std::unique_ptr<ExprAST> ParseParenExpr() {
 ///   ::= identifier
 ///   ::= identifier '(' expression* ')'
 static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
-  std::string IdName = IdentifierStr;
+  std::string IdName = LexerObjects::IdentifierStr;
 
   getNextToken(); // eat identifier.
 
@@ -166,7 +166,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() {
   if (CurTok != tok_identifier)
     return ErrorP("Expected function name in prototype");
 
-  std::string FnName = IdentifierStr;
+  std::string FnName = LexerObjects::IdentifierStr;
   getNextToken();
 
   if (CurTok != '(')
@@ -174,7 +174,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() {
 
   std::vector<std::string> ArgNames;
   while (getNextToken() == tok_identifier)
-    ArgNames.push_back(IdentifierStr);
+    ArgNames.push_back(LexerObjects::IdentifierStr);
   if (CurTok != ')')
     return ErrorP("Expected ')' in prototype");
 
@@ -200,8 +200,7 @@ static std::unique_ptr<FunctionAST> ParseDefinition() {
 static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
   if (auto E = ParseExpression()) {
     // Make an anonymous proto.
-    auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr",
-                                                 std::vector<std::string>());
+    auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", std::vector<std::string>());
     return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
   }
   return nullptr;

+ 2 - 2
source/Parser.h

@@ -1,5 +1,5 @@
-#ifndef PARSER_H
-#define PARSER_H
+#ifndef _PARSER_H
+#define _PARSER_H
 
 #include <cstdio>
 #include <map>