#include "llvm/Support/TargetSelect.h"
#include "llvm/Transforms/Scalar.h"
#include <cctype>
-#include <cstdio>
+#include <iomanip>
+#include <iostream>
#include <map>
+#include <sstream>
#include <string>
#include <vector>
using namespace llvm;
}
template <typename T>
-std::unique_ptr<T> ErrorU(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+std::unique_ptr<T> ErrorU(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
template <typename T>
-T* ErrorP(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+T* ErrorP(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
//===----------------------------------------------------------------------===//
// FIXME: Obviously we can do better than this
-std::string GenerateUniqueName(const char *root)
-{
+std::string GenerateUniqueName(const std::string &Root) {
static int i = 0;
- char s[16];
- sprintf(s, "%s%d", root, i++);
- std::string S = s;
- return S;
+ std::ostringstream NameStream;
+ NameStream << Root << ++i;
+ return NameStream.str();
}
std::string MakeLegalFunctionName(std::string Name)
std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
size_t pos;
while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
- char old_c = NewName.at(pos);
- char new_str[16];
- sprintf(new_str, "%d", (int)old_c);
- NewName = NewName.replace(pos, 1, new_str);
+ std::ostringstream NumStream;
+ NumStream << (int)NewName.at(pos);
+ NewName = NewName.replace(pos, 1, NumStream.str());
}
return NewName;
if (I != Prototypes.end())
return I->second.get();
return nullptr;
-}
+}
class IRGenContext {
public:
// Look this variable up in the function.
Value *V = C.NamedValues[Name];
- if (V == 0) {
- char ErrStr[256];
- sprintf(ErrStr, "Unknown variable name %s", Name.c_str());
- return ErrorP<Value>(ErrStr);
- }
+ if (V == 0)
+ return ErrorP<Value>("Unknown variable name '" + Name + "'");
// Load the value.
return C.getBuilder().CreateLoad(V, Name.c_str());
IRGenContext C(S);
if (auto LF = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "Read function definition:");
+ std::cerr << "Read function definition:\n";
LF->dump();
#endif
J.addModule(C.takeM());
IRGenContext C(S);
if (auto ExprFunc = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "Expression function:\n");
+ std::cerr << "Expression function:\n";
ExprFunc->dump();
#endif
// Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
#ifdef MINIMAL_STDERR_OUTPUT
FP();
#else
- fprintf(stderr, "Evaluated to %f\n", FP());
+ std::cerr << "Evaluated to " << FP() << "\n";
#endif
// Remove the function.
while (1) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
switch (CurTok) {
case tok_eof: return;
// Prime the first token.
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
getNextToken();
+ std::cerr << std::fixed;
+
// Run the main "interpreter loop" now.
MainLoop();
#include "llvm/Support/TargetSelect.h"
#include "llvm/Transforms/Scalar.h"
#include <cctype>
-#include <cstdio>
+#include <iomanip>
+#include <iostream>
#include <map>
+#include <sstream>
#include <string>
#include <vector>
using namespace llvm;
}
template <typename T>
-std::unique_ptr<T> ErrorU(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+std::unique_ptr<T> ErrorU(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
template <typename T>
-T* ErrorP(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+T* ErrorP(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
//===----------------------------------------------------------------------===//
// FIXME: Obviously we can do better than this
-std::string GenerateUniqueName(const char *root)
-{
+std::string GenerateUniqueName(const std::string &Root) {
static int i = 0;
- char s[16];
- sprintf(s, "%s%d", root, i++);
- std::string S = s;
- return S;
+ std::ostringstream NameStream;
+ NameStream << Root << ++i;
+ return NameStream.str();
}
std::string MakeLegalFunctionName(std::string Name)
std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
size_t pos;
while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
- char old_c = NewName.at(pos);
- char new_str[16];
- sprintf(new_str, "%d", (int)old_c);
- NewName = NewName.replace(pos, 1, new_str);
+ std::ostringstream NumStream;
+ NumStream << (int)NewName.at(pos);
+ NewName = NewName.replace(pos, 1, NumStream.str());
}
return NewName;
if (I != Prototypes.end())
return I->second.get();
return nullptr;
-}
+}
class IRGenContext {
public:
// Look this variable up in the function.
Value *V = C.NamedValues[Name];
- if (V == 0) {
- char ErrStr[256];
- sprintf(ErrStr, "Unknown variable name %s", Name.c_str());
- return ErrorP<Value>(ErrStr);
- }
+ if (V == 0)
+ return ErrorP<Value>("Unknown variable name '" + Name + "'");
// Load the value.
return C.getBuilder().CreateLoad(V, Name.c_str());
IRGenContext C(S);
if (auto LF = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "Read function definition:");
+ std::cerr << "Read function definition:\n";
LF->dump();
#endif
J.addModule(C.takeM());
IRGenContext C(S);
if (auto ExprFunc = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "Expression function:\n");
+ std::cerr << "Expression function:\n";
ExprFunc->dump();
#endif
// Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
#ifdef MINIMAL_STDERR_OUTPUT
FP();
#else
- fprintf(stderr, "Evaluated to %f\n", FP());
+ std::cerr << "Evaluated to " << FP() << "\n";
#endif
// Remove the function.
while (1) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
switch (CurTok) {
case tok_eof: return;
// Prime the first token.
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
getNextToken();
+ std::cerr << std::fixed;
+
// Run the main "interpreter loop" now.
MainLoop();
#include "llvm/Support/TargetSelect.h"
#include "llvm/Transforms/Scalar.h"
#include <cctype>
-#include <cstdio>
+#include <iomanip>
+#include <iostream>
#include <map>
+#include <sstream>
#include <string>
#include <vector>
using namespace llvm;
}
template <typename T>
-std::unique_ptr<T> ErrorU(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+std::unique_ptr<T> ErrorU(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
template <typename T>
-T* ErrorP(const char *Str) {
- fprintf(stderr, "Error: %s\n", Str);
+T* ErrorP(const std::string &Str) {
+ std::cerr << "Error: " << Str << "\n";
return nullptr;
}
//===----------------------------------------------------------------------===//
// FIXME: Obviously we can do better than this
-std::string GenerateUniqueName(const char *root)
-{
+std::string GenerateUniqueName(const std::string &Root) {
static int i = 0;
- char s[16];
- sprintf(s, "%s%d", root, i++);
- std::string S = s;
- return S;
+ std::ostringstream NameStream;
+ NameStream << Root << ++i;
+ return NameStream.str();
}
std::string MakeLegalFunctionName(std::string Name)
std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
size_t pos;
while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
- char old_c = NewName.at(pos);
- char new_str[16];
- sprintf(new_str, "%d", (int)old_c);
- NewName = NewName.replace(pos, 1, new_str);
+ std::ostringstream NumStream;
+ NumStream << (int)NewName.at(pos);
+ NewName = NewName.replace(pos, 1, NumStream.str());
}
return NewName;
if (I != Prototypes.end())
return I->second.get();
return nullptr;
-}
+}
class IRGenContext {
public:
// Look this variable up in the function.
Value *V = C.NamedValues[Name];
- if (V == 0) {
- char ErrStr[256];
- sprintf(ErrStr, "Unknown variable name %s", Name.c_str());
- return ErrorP<Value>(ErrStr);
- }
+ if (V == 0)
+ return ErrorP<Value>("Unknown variable name '" + Name + "'");
// Load the value.
return C.getBuilder().CreateLoad(V, Name.c_str());
IRGenContext C(S);
if (auto ExprFunc = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "Expression function:\n");
+ std::cerr << "Expression function:\n";
ExprFunc->dump();
#endif
// Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
#ifdef MINIMAL_STDERR_OUTPUT
FP();
#else
- fprintf(stderr, "Evaluated to %f\n", FP());
+ std::cerr << "Evaluated to " << FP() << "\n";
#endif
// Remove the function.
while (1) {
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
switch (CurTok) {
case tok_eof: return;
// Prime the first token.
#ifndef MINIMAL_STDERR_OUTPUT
- fprintf(stderr, "ready> ");
+ std::cerr << "ready> ";
#endif
getNextToken();
+ std::cerr << std::fixed;
+
// Run the main "interpreter loop" now.
MainLoop();