some error reporting

main
Clyne 6 months ago
parent 6991c3986b
commit ed78a6054c
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

@ -8,7 +8,7 @@ main: $(subst .cpp,.o,$(CXXFILES))
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
prog: main test.fp 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 llc -march=x86 -filetype=obj --relocation-model=pic forsp.ir -O1
clang -c support.c -m32 -Os clang -c support.c -m32 -Os
clang support.o forsp.ir.o -m32 -Os clang support.o forsp.ir.o -m32 -Os

@ -17,14 +17,25 @@
*/ */
#include "ast.hpp" #include "ast.hpp"
#include <charconv>
#include <iostream>
int ThunkAST::tcount = 0; int ThunkAST::tcount = 0;
NumberAST::NumberAST(const std::string& n): BaseAST(n) {} NumberAST::NumberAST(const std::string& n): BaseAST(n) {}
llvm::Value *NumberAST::codegen(LLVMState& llvmState) const llvm::Value *NumberAST::codegen(LLVMState& llvmState) const
{ {
auto val = llvmState.createInt(std::stoi(name)); 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()); return llvmState.builder.CreateStore(val, llvmState.createPush());
}
} }
PushAST::PushAST(const std::string& n): BaseAST(n) {} 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); return llvmState.builder.CreateStore(var, dsget);
} else { } else {
std::cerr << "error: not defined: " << name << std::endl;
return nullptr; return nullptr;
} }
} }
@ -68,6 +80,8 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
return llvmState.builder.CreateCall(llvmState.ftype, cast); return llvmState.builder.CreateCall(llvmState.ftype, cast);
} }
} else { } else {
std::cerr << "warning: anticipating external function: "
<< name << std::endl;
auto func = llvmState.createFunction(name); auto func = llvmState.createFunction(name);
Var::addGlobal(name, Var {func, true}); Var::addGlobal(name, Var {func, true});
return llvmState.builder.CreateCall(llvmState.ftype, func); return llvmState.builder.CreateCall(llvmState.ftype, func);

@ -76,6 +76,5 @@ llvm::Constant *LLVMState::createInt(int n)
void LLVMState::output() void LLVMState::output()
{ {
//std::cout << "LLVM:" << std::endl; modul.print(llvm::outs(), nullptr);
modul.print(llvm::errs(), nullptr);
} }

@ -40,14 +40,13 @@ int main()
Var::pushScope(); Var::pushScope();
std::string line; std::string line;
while (std::cin.good()) { for (unsigned lineno = 1; std::cin.good(); ++lineno) {
std::getline(std::cin, line); std::getline(std::cin, line);
parseString(line); if (!parseString(line))
//std::cout << std::endl; std::cerr << " at line " << lineno << std::endl;
} }
llvmState.output(); llvmState.output();
std::cout << std::endl; std::cout << std::endl;
} }
@ -56,11 +55,9 @@ bool parseString(std::string_view sv)
do { do {
const auto [nsv, tok] = nextToken(sv); const auto [nsv, tok] = nextToken(sv);
//printToken(tok);
if (tok == Token::none && !nsv.empty()) { if (tok == Token::none && !nsv.empty()) {
std::cerr << "unknown " << nsv << std::endl; std::cerr << "error: unknown term: " << nsv << std::endl;
break; return false;
} else { } else {
std::unique_ptr<BaseAST> expr; std::unique_ptr<BaseAST> expr;
@ -76,6 +73,8 @@ bool parseString(std::string_view sv)
{ {
auto& thunk = scope.back(); auto& thunk = scope.back();
auto gen = thunk.codegen(llvmState); auto gen = thunk.codegen(llvmState);
if (!gen)
return false;
Var::popScope(); Var::popScope();
Var::addLocal(thunk.name, Var {gen, true}); Var::addLocal(thunk.name, Var {gen, true});
expr.reset(new PushAST {thunk.name}); expr.reset(new PushAST {thunk.name});
@ -83,7 +82,8 @@ bool parseString(std::string_view sv)
} }
break; break;
case Token::Quote: case Token::Quote:
break; std::cerr << "error: quoting is not supported!" << std::endl;
return false;
case Token::PopVar: case Token::PopVar:
expr.reset(new PopAST {name}); expr.reset(new PopAST {name});
break; break;
@ -100,9 +100,14 @@ bool parseString(std::string_view sv)
break; break;
} }
if (expr && !scope.empty()) { if (expr) {
expr->codegen(llvmState); if (!scope.empty()) {
//scope.back().body.emplace_back().swap(expr); if (!expr->codegen(llvmState))
return false;
} else if (tok != Token::ThunkClose) {
std::cerr << "error: non-thunk at top level!" << std::endl;
return false;
}
} }
} }

Loading…
Cancel
Save