]> git.leonardobizzoni.com Git - LBPL/commitdiff
Fixed typos and error in lexer EOF checks
authorLeonardoBizzoni <leo2002714@gmail.com>
Fri, 6 Sep 2024 14:27:16 +0000 (16:27 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Fri, 6 Sep 2024 14:27:16 +0000 (16:27 +0200)
main.lbpl
src/AST-generation/lexer.cpp
src/AST-generation/parser.cpp
src/interpretation/builtin_methods.hpp
src/interpretation/interpreter.cpp

index d1828467022adc4fbc0eaa05f12289f9c9f0b756..46c8dbef51c004bc8de28fd8b1c493ffeef37f78 100644 (file)
--- a/main.lbpl
+++ b/main.lbpl
@@ -1,15 +1,15 @@
 fn fib(n) {
-   return fibIter(n, 0, 1);
-}
-
-fn fibIter(n, curr, next) {
-   if (n == 0) {
-      return curr;
-   } else if (n == 1) {
-     return next;
-   } else {
-     return fibIter(n - 1, next, curr + next);
+   fn fibIter(n, curr, next) {
+      if (n == 0) {
+        return curr;
+      } else if (n == 1) {
+       return next;
+      } else {
+       return fibIter(n - 1, next, curr + next);
+      }
    }
+
+   return fibIter(n, 0, 1);
 }
 
 class Prova {
@@ -34,4 +34,4 @@ println(a.var1);
 a.var1 = 210;
 println(a.var1);
 
-a.runtime();
\ No newline at end of file
+a.runtime();
index 8696a38059e8d412b1c2bc0f0d18bf4ccf55dc80..57c3c6d35a4c617bfd0f5eaa4599e0d6903c4c7a 100755 (executable)
@@ -121,7 +121,7 @@ std::shared_ptr<const Token> getNextToken(Source &file) {
     char *lexeme = 0;
     char new_ch = 0;
 
-    while (file.stream.eof() && file.stream.peek() != '"') {
+    while (file.stream.peek() != EOF && file.stream.peek() != '"') {
       if (file.stream.peek() == '\\') {
         file.advance();
 
@@ -157,7 +157,7 @@ std::shared_ptr<const Token> getNextToken(Source &file) {
       lexeme[len - 1] = new_ch;
     }
 
-    if (file.stream.eof()) {
+    if (file.stream.peek() == EOF) {
       return MAKE_TOKEN(TokenType::Error, "Unterminated string.");
     } else {
       file.advance();
@@ -256,7 +256,7 @@ TokenType isIdentifierOrKeyword(const char *lexeme, size_t len) {
     if (len > 1) {
       switch (lexeme[1]) {
       case 'l':
-        return checkKeyword(lexeme, len, 2, "ass", TokenType::Break);
+        return checkKeyword(lexeme, len, 2, "ass", TokenType::Class);
       case 'o':
         return checkKeyword(lexeme, len, 2, "ntinue", TokenType::Continue);
       }
index 239f921a5c51007ac11deecb6ccb7d52a3055427..b4e43f05a78a3248c475961c2056a0798c923b93 100755 (executable)
@@ -1,12 +1,14 @@
 #include "parser.hpp"
 #include "syntax_error.hpp"
 
+#include <cstdint>
 #include <fcntl.h>
 #include <fstream>
 #include <iostream>
 #include <string>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <variant>
 
 std::vector<std::unique_ptr<Stmt>> Parser::parse() {
   std::vector<std::unique_ptr<Stmt>> stmts;
@@ -279,7 +281,7 @@ std::unique_ptr<Expr> Parser::expression() {
   if (match(TokenType::Break)) {
     return std::make_unique<BreakExpr>(current->line, current->column,
                                        current->filename);
-  } else if (match(TokenType::Break)) {
+  } else if (match(TokenType::Continue)) {
     return std::make_unique<ContinueExpr>(current->line, current->column,
                                           current->filename);
   }
index 0ded93d3686223870c38b9aa9edd3916d57109d6..9e5e930e5ce271907d84e54bed34ecf56ef1ffe5 100644 (file)
@@ -11,7 +11,7 @@ class LBPLPrintln : public LBPLCallable {
 public:
   LBPLPrintln() {}
 
-  int arity() override { return 1; };
+  constexpr int arity() override { return 1; };
 
   Value call(Interpreter *, std::vector<Value> &args) override {
     if (std::holds_alternative<std::string>(args[0])) {
@@ -34,7 +34,7 @@ class LBPLClock : public LBPLCallable {
 public:
   LBPLClock() {}
 
-  int arity() override { return 0; };
+  constexpr int arity() override { return 0; };
 
   Value call(Interpreter *, std::vector<Value> &args) override {
     return std::chrono::duration_cast<std::chrono::milliseconds>(
index 53ecdd373b0fba5b70ffb32cc39e799772d6d96e..096c4be31947f94c022df9213bcc8134527b675f 100644 (file)
@@ -1,9 +1,9 @@
 #include "interpreter.hpp"
+#include "runtime_error.hpp"
 #include "types/LBPLClass.hpp"
 #include "types/LBPLFunction.hpp"
 #include "types/LBPLInstance.hpp"
 #include "types/LBPLTypes.hpp"
-#include "runtime_error.hpp"
 
 #include <cstddef>
 #include <cstdint>