aboutsummaryrefslogtreecommitdiff
path: root/include/cps
diff options
context:
space:
mode:
author_ov4 <hausemasterstream@gmail.com>2026-02-02 10:08:22 +0000
committer_ov4 <hausemasterstream@gmail.com>2026-02-02 10:08:22 +0000
commit1b524f75401a6318fbaf3fee0bfcbe59348f2e51 (patch)
tree2616ebfc5e03f74f5cf33aab632c90da96d4fb44 /include/cps
parent09e128ce98de793d22669255897e4b4816d0e53e (diff)
Added STRING
Diffstat (limited to 'include/cps')
-rw-r--r--include/cps/AST.h7
-rw-r--r--include/cps/CodeGen.h3
-rw-r--r--include/cps/Lexer.h9
-rw-r--r--include/cps/Parser.h3
-rw-r--r--include/cps/StringHandler.h38
5 files changed, 60 insertions, 0 deletions
diff --git a/include/cps/AST.h b/include/cps/AST.h
index de57024..f07e937 100644
--- a/include/cps/AST.h
+++ b/include/cps/AST.h
@@ -32,6 +32,13 @@ public:
bool getVal() const { return Val; }
};
+class StringExprAST : public ExprAST {
+ std::string Val;
+public:
+ StringExprAST(const std::string &Val) : Val(Val) {}
+ const std::string &getVal() const { return Val; }
+};
+
class VariableExprAST : public ExprAST {
std::string Name;
public:
diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h
index df9eef4..cb8cecb 100644
--- a/include/cps/CodeGen.h
+++ b/include/cps/CodeGen.h
@@ -10,6 +10,7 @@
#include "cps/RealHandler.h"
#include "cps/BooleanHandler.h"
#include "cps/ArithmeticHandler.h"
+#include "cps/StringHandler.h"
#include <map>
#include <memory>
@@ -32,6 +33,7 @@ class CodeGen {
std::unique_ptr<RealHandler> RealHelper;
std::unique_ptr<BooleanHandler> BoolHandler;
std::unique_ptr<ArithmeticHandler> ArithHandler;
+ std::unique_ptr<StringHandler> StrHandler;
llvm::FunctionCallee PrintfFunc;
llvm::FunctionCallee ScanfFunc;
@@ -42,6 +44,7 @@ class CodeGen {
llvm::Value *ScanfFormatStr; // %lld
llvm::Value *ScanfFloatFormatStr; // %lf
+ llvm::Value *ScanfStringFormatStr; // %s
llvm::Value *TrueStr; // "TRUE"
llvm::Value *FalseStr; // "FALSE"
diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h
index 97ccd83..c36599f 100644
--- a/include/cps/Lexer.h
+++ b/include/cps/Lexer.h
@@ -41,6 +41,14 @@ enum Token {
tok_or = -26,
tok_not = -27,
+ tok_string_kw = -30,
+ tok_length = -31,
+ tok_mid = -32,
+ tok_right = -33,
+ tok_lcase = -34,
+ tok_ucase = -35,
+ tok_string_literal = -36,
+
tok_identifier = -100,
tok_number_int = -101,
tok_number_real = -102,
@@ -71,6 +79,7 @@ class Lexer {
int CurrentLine = 1;
public:
std::string IdentifierStr;
+ std::string StringVal;
int64_t NumVal;
double RealVal;
diff --git a/include/cps/Parser.h b/include/cps/Parser.h
index db1f243..877c684 100644
--- a/include/cps/Parser.h
+++ b/include/cps/Parser.h
@@ -4,6 +4,7 @@
#include <vector>
#include <memory>
#include <map>
+#include <tuple>
namespace cps {
@@ -24,6 +25,8 @@ class Parser {
std::unique_ptr<ExprAST> ParseNumberExpr();
std::unique_ptr<ExprAST> ParseIdentifierExpr();
std::unique_ptr<ExprAST> ParseParenExpr();
+
+ std::unique_ptr<ExprAST> ParseStringBuiltin(const std::string &FuncName);
std::unique_ptr<StmtAST> ParseStatement();
std::unique_ptr<StmtAST> ParseIfStmt();
diff --git a/include/cps/StringHandler.h b/include/cps/StringHandler.h
new file mode 100644
index 0000000..87710fb
--- /dev/null
+++ b/include/cps/StringHandler.h
@@ -0,0 +1,38 @@
+#pragma once
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <map>
+#include <string>
+
+namespace cps {
+
+class StringHandler {
+ llvm::LLVMContext &Context;
+ llvm::IRBuilder<> &Builder;
+ llvm::Module &Module;
+ std::map<std::string, llvm::Value*> &NamedValues;
+
+ llvm::FunctionCallee MallocFunc;
+ llvm::FunctionCallee FreeFunc;
+ llvm::FunctionCallee StrLenFunc;
+ llvm::FunctionCallee MemCpyFunc;
+ llvm::FunctionCallee ToUpperFunc;
+ llvm::FunctionCallee ToLowerFunc;
+
+public:
+ StringHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M,
+ std::map<std::string, llvm::Value*> &NV);
+
+ void setupExternalFunctions();
+ void emitDeclare(const std::string &Name);
+ llvm::Value *createLiteral(const std::string &Val);
+
+ llvm::Value *emitLength(llvm::Value *Str);
+ llvm::Value *emitMid(llvm::Value *Str, llvm::Value *Start, llvm::Value *Len);
+ llvm::Value *emitRight(llvm::Value *Str, llvm::Value *Len);
+ llvm::Value *emitLCase(llvm::Value *Str);
+ llvm::Value *emitUCase(llvm::Value *Str);
+};
+
+}