]> code.bitgloo.com Git - clyne/forspll.git/commitdiff
some error reporting
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 15 Jun 2024 15:54:19 +0000 (11:54 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 15 Jun 2024 15:54:19 +0000 (11:54 -0400)
Makefile
ast.cpp
llvm.cpp
main.cpp

index c7761c018b84605d464d7e3f5858b21716741798..caa1558868f9546dddc41565b3d42056ffa29783 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ main: $(subst .cpp,.o,$(CXXFILES))
        $(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
 
 prog: main test.fp
-       ./main < test.fp 2> forsp.ir
+       ./main < test.fp > forsp.ir
        llc -march=x86 -filetype=obj --relocation-model=pic forsp.ir -O1
        clang -c support.c -m32 -Os
        clang support.o forsp.ir.o -m32 -Os
diff --git a/ast.cpp b/ast.cpp
index 1de183ea109249e5aa3b2d697c0863bea83a1bd8..3543ee1a366c7035f29f519387ee05daba9a668b 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
  */
 #include "ast.hpp"
 
+#include <charconv>
+#include <iostream>
+
 int ThunkAST::tcount = 0;
 
 NumberAST::NumberAST(const std::string& n): BaseAST(n) {}
 
 llvm::Value *NumberAST::codegen(LLVMState& llvmState) const
 {
-    auto val = llvmState.createInt(std::stoi(name));
-    return llvmState.builder.CreateStore(val, llvmState.createPush());
+    int value;
+    auto [ptr, _] = std::from_chars(&name.front(), &name.back() + 1, value);
+
+    if (ptr <= &name.back()) {
+        std::cerr << "error: not a number: " << name << std::endl;
+        return nullptr;
+    } else {
+        auto val = llvmState.createInt(value);
+        return llvmState.builder.CreateStore(val, llvmState.createPush());
+    }
 }
 
 PushAST::PushAST(const std::string& n): BaseAST(n) {}
@@ -38,6 +49,7 @@ llvm::Value *PushAST::codegen(LLVMState& llvmState) const
 
         return llvmState.builder.CreateStore(var, dsget);
     } else {
+        std::cerr << "error: not defined: " << name << std::endl;
         return nullptr;
     }
 }
@@ -68,6 +80,8 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
             return llvmState.builder.CreateCall(llvmState.ftype, cast);
         }
     } else {
+        std::cerr << "warning: anticipating external function: "
+            << name << std::endl;
         auto func = llvmState.createFunction(name);
         Var::addGlobal(name, Var {func, true});
         return llvmState.builder.CreateCall(llvmState.ftype, func);
index e5ed2db90bdca64240e029b50ccae166b79f8f2c..f1ee27aac95275a01016febc3aaf31eced55c094 100644 (file)
--- a/llvm.cpp
+++ b/llvm.cpp
@@ -76,6 +76,5 @@ llvm::Constant *LLVMState::createInt(int n)
 
 void LLVMState::output()
 {
-    //std::cout << "LLVM:" << std::endl;
-    modul.print(llvm::errs(), nullptr);
+    modul.print(llvm::outs(), nullptr);
 }
index f2c0b1d065a3d8f8bb2ddb4949621289d0660ba5..a632c9b9a4ec597854d30a90483740e94faa2a5a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -40,14 +40,13 @@ int main()
     Var::pushScope();
 
     std::string line;
-    while (std::cin.good()) {
+    for (unsigned lineno = 1; std::cin.good(); ++lineno) {
         std::getline(std::cin, line);
-        parseString(line);
-        //std::cout << std::endl;
+        if (!parseString(line))
+            std::cerr << "    at line " << lineno << std::endl;
     }
 
     llvmState.output();
-
     std::cout << std::endl;
 }
 
@@ -56,11 +55,9 @@ bool parseString(std::string_view sv)
     do {
         const auto [nsv, tok] = nextToken(sv);
 
-        //printToken(tok);
-
         if (tok == Token::none && !nsv.empty()) {
-            std::cerr << "unknown " << nsv << std::endl;
-            break;
+            std::cerr << "error: unknown term: " << nsv << std::endl;
+            return false;
         } else {
             std::unique_ptr<BaseAST> expr;
 
@@ -76,6 +73,8 @@ bool parseString(std::string_view sv)
                 {
                 auto& thunk = scope.back();
                 auto gen = thunk.codegen(llvmState);
+                if (!gen)
+                    return false;
                 Var::popScope();
                 Var::addLocal(thunk.name, Var {gen, true});
                 expr.reset(new PushAST {thunk.name});
@@ -83,7 +82,8 @@ bool parseString(std::string_view sv)
                 }
                 break;
             case Token::Quote:
-                break;
+                std::cerr << "error: quoting is not supported!" << std::endl;
+                return false;
             case Token::PopVar:
                 expr.reset(new PopAST {name});
                 break;
@@ -100,9 +100,14 @@ bool parseString(std::string_view sv)
                 break;
             }
 
-            if (expr && !scope.empty()) {
-                expr->codegen(llvmState);
-                //scope.back().body.emplace_back().swap(expr);
+            if (expr) {
+                if (!scope.empty()) {
+                    if (!expr->codegen(llvmState))
+                        return false;
+                } else if (tok != Token::ThunkClose) {
+                    std::cerr << "error: non-thunk at top level!" << std::endl;
+                    return false;
+                }
             }
         }