First commit

This commit is contained in:
2026-01-23 05:20:23 -06:00
commit a2205a44f1
10 changed files with 2086 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

34
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,34 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bcrossc",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

14
CMakeLists.txt Normal file
View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.16)
project(BetterCrossLangCompiler)
find_package(TessesCrossLang REQUIRED)
add_executable(bcrossc
src/bcrossc.cpp
src/node.cpp
src/parser.cpp
)
target_link_libraries(bcrossc PUBLIC TessesCrossLang::crosslang_shared)

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
I AM STILL WORKING ON THIS,
DON'T CLONE

0
result.json Normal file
View File

120
sourceCode.btcross Normal file
View File

@@ -0,0 +1,120 @@
/*var mylist : List<of Long>?;
var str : String = "My Text" + "is" + "Demi Lovato";
var myLs : List<of List<of String>>?;
var nl : Char = '\n';
delegate Action<of T>(a: T);
delegate Func<of T, TResult>(a: T): TResult;
var a = 42;
var p : Path = "";
func main(args: List<of String>): Long
{
return 42;
}
func times<of T>(a: T, b: T, typ: Type): T
{
return a * b;
}
/^
my doc
^/
func a(arg: String)
{
if(times<of Long>(6,7) == 42)
{
Console.WriteLine($"It is the meaning of life {arg}");
var fs : LocalFilesystem = FS.Local;
var path: Path = "/mypath";
}
each(var v : Char in "Demi Lovato")
{
}
for(var i : Long = 0; i < 42; i++)
{
}
}
func b(a,b)
{
var c = 52;
c += a;
c += b;
return c;
}
func c(a: Long, b: Long): Long
{
var c : Long = 52;
c += a;
c += b;
return c;
}
func d()
{
var e : Func<of Long, Long, Long> = (a: Long, b: Long) => : Long {
Console.WriteLine(a + b);
return 42;
};
var file: ByteArray = embed("demi.bin");
}
namespace FS {
extern var Local: LocalFilesystem;
}
use FS;
interface IAddable<of T> {
func add(val: T): T;
}
class Add : IAddable<of Long> {
private val: Long
public func Add(a: Long)
{
this.val = a;
}
public func add(a: Long): Long
{
return this.val + a;
}
}
*/
/^
The filesystem API
^/
namespace FS {
/^
The local filesystem
^/
extern var Local: LocalFilesystem;
}
/^
My interface
^/
interface MyInterface<of T> : IOtherInterface<of T, String>, IAN {
/^
Me
@name "Steve"
^/
func doThing(a: T): Long;
}

19
src/bcrossc.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "bcrossc.hpp"
#include <iostream>
#include <fstream>
using namespace Tesses::Framework;
using namespace Tesses::CrossLang;
int main(int argc, char** argv)
{
TF_Init();
std::ifstream strm("../sourceCode.btcross",std::ios_base::binary);
std::vector<LexToken> tokens;
Lex("sourceCode.btcross",strm, tokens);
BCParser parser(tokens);
auto node = parser.ParseRoot();
auto json = node->ToJsonObject();
std::cout << Tesses::Framework::Serialization::Json::Json::Encode(json) << std::endl;
}

296
src/bcrossc.hpp Normal file
View File

@@ -0,0 +1,296 @@
#include <CrossLang.hpp>
namespace Tesses::CrossLang {
using TF_JToken = Tesses::Framework::Serialization::Json::JToken;
class BNode {
public:
virtual bool IsExpression() = 0;
virtual ~BNode() = default;
virtual TF_JToken ToJsonObject() = 0;
};
using BSyntaxNode = std::shared_ptr<BNode>;
struct BType {
std::vector<std::string> nameParts;
std::vector<BType> generics;
bool nullable=false;
std::string Stringify();
};
class BUndefined : public BNode {
public:
bool IsExpression();
TF_JToken ToJsonObject();
};
class BNull : public BNode {
public:
bool IsExpression();
TF_JToken ToJsonObject();
};
class BString : public BNode {
public:
BString(std::string text);
bool IsExpression();
TF_JToken ToJsonObject();
std::string text;
};
class BChar : public BNode {
public:
BChar(char value);
bool IsExpression();
TF_JToken ToJsonObject();
char value;
};
class BBoolean : public BNode {
public:
BBoolean(bool text);
bool IsExpression();
TF_JToken ToJsonObject();
bool value;
};
class BLong : public BNode {
public:
BLong(int64_t text);
bool IsExpression();
TF_JToken ToJsonObject();
int64_t value;
};
class BDouble : public BNode {
public:
BDouble(double text);
bool IsExpression();
TF_JToken ToJsonObject();
double value;
};
class BUnaryExpression : public BNode {
public:
BUnaryExpression(std::string_view type, BSyntaxNode val);
bool IsExpression();
TF_JToken ToJsonObject();
std::string_view type;
BSyntaxNode value;
};
class BBinaryExpression : public BNode {
public:
BBinaryExpression(std::string_view type, BSyntaxNode left, BSyntaxNode right);
bool IsExpression();
TF_JToken ToJsonObject();
std::string_view type;
BSyntaxNode left;
BSyntaxNode right;
};
class BTernaryExpression : public BNode {
public:
BTernaryExpression(BSyntaxNode cond, BSyntaxNode yes, BSyntaxNode no);
bool IsExpression();
TF_JToken ToJsonObject();
BSyntaxNode cond;
BSyntaxNode yes;
BSyntaxNode no;
};
class BIfStatement : public BNode {
public:
BIfStatement(BSyntaxNode cond, BSyntaxNode yes, BSyntaxNode no);
bool IsExpression();
TF_JToken ToJsonObject();
BSyntaxNode cond;
BSyntaxNode yes;
BSyntaxNode no;
};
class BEachStatement : public BNode {
public:
BEachStatement(BSyntaxNode var, BSyntaxNode list, BSyntaxNode body);
bool IsExpression();
TF_JToken ToJsonObject();
BSyntaxNode var;
BSyntaxNode list;
BSyntaxNode body;
};
class BExternVariableNode : public BNode {
public:
BExternVariableNode(std::string documentation,std::string name, BType type);
bool IsExpression();
TF_JToken ToJsonObject();
std::string documentation;
std::string name;
BType type;
};
class BExternFuncNode : public BNode {
public:
BExternFuncNode(std::string documentation,BType returnType, BType name, BSyntaxNode args);
bool IsExpression();
TF_JToken ToJsonObject();
std::string documentation;
BType returnType;
BType name;
BSyntaxNode args;
};
class BNamespaceNode : public BNode {
public:
BNamespaceNode(BType name, BSyntaxNode body);
bool IsExpression();
TF_JToken ToJsonObject();
BType name;
BSyntaxNode body;
};
class BUsingType : public BNode {
public:
BUsingType(BType name, BType type);
bool IsExpression();
TF_JToken ToJsonObject();
BType name;
BType type;
};
class BUsingNamespace : public BNode {
public:
BUsingNamespace(BType name);
bool IsExpression();
TF_JToken ToJsonObject();
BType name;
};
class BVariableDeclarationNode : public BNode {
public:
BVariableDeclarationNode(std::string documetnation,std::string name, BType type, BSyntaxNode value);
std::string documentation;
std::string name;
BType type;
BSyntaxNode value;
bool IsExpression();
TF_JToken ToJsonObject();
};
class BArgumentNode : public BNode {
public:
BArgumentNode(std::string name, BType type);
std::string name;
BType type;
bool IsExpression();
TF_JToken ToJsonObject();
};
class BGetVariableExpression : public BNode {
public:
BGetVariableExpression(std::string name);
std::string name;
bool IsExpression();
TF_JToken ToJsonObject();
};
class BScopeNode : public BNode {
bool isExpression;
bool isRoot;
public:
BScopeNode(bool isRoot=false,bool isExpression=false);
bool IsRoot();
bool IsExpression();
std::vector<BSyntaxNode> childern;
TF_JToken ToJsonObject();
};
class BFunctionNode : public BNode {
public:
BFunctionNode(std::string documentation,BType returnType, BType name, BSyntaxNode args, BSyntaxNode body);
bool IsExpression();
TF_JToken ToJsonObject();
std::string documentation;
BType returnType;
BType name;
BSyntaxNode args;
BSyntaxNode body;
};
class BClosureExpression : public BNode {
public:
BClosureExpression(BType returnType, BSyntaxNode args, BSyntaxNode body);
bool IsExpression();
TF_JToken ToJsonObject();
BType returnType;
BSyntaxNode args;
BSyntaxNode body;
};
class BFunctionCallExpression : public BNode {
public:
BFunctionCallExpression(BSyntaxNode fun, BSyntaxNode args, std::vector<BType> generics);
bool IsExpression();
TF_JToken ToJsonObject();
BSyntaxNode fun;
BSyntaxNode args;
std::vector<BType> generics;
};
class BDelegateNode : public BNode {
public:
BDelegateNode(std::string documentation,BType returnType, BType name, BSyntaxNode args);
bool IsExpression();
TF_JToken ToJsonObject();
std::string documentation;
BType returnType;
BType name;
BSyntaxNode args;
};
class BInterfaceNode : public BNode {
public:
BInterfaceNode(std::string documentation, BType name);
bool IsExpression();
TF_JToken ToJsonObject();
std::string documentation;
BType name;
std::vector<BType> inherits;
std::vector<BFunctionNode> funs;
};
class BReturnNode : public BNode {
public:
BReturnNode(BSyntaxNode retVal);
BSyntaxNode retVal;
bool IsExpression();
TF_JToken ToJsonObject();
};
class BCParser {
size_t i = 0;
std::vector<LexToken> tokens;
BSyntaxNode ParseNode(bool isRoot=false);
LexToken tkn;
void EnsureSymbol(std::string txt);
bool IsIdentifier(std::string txt,bool pop=true);
bool IsAnyIdentifier(std::initializer_list<std::string> idents, bool pop=true);
bool IsSymbol(std::string txt,bool pop=true);
bool IsAnySymbol(std::initializer_list<std::string> idents, bool pop=true);
std::string ReadIdentifier();
BType ReadType();
bool IsGeneric();
void ReadGenerics(std::vector<BType>& generics);
BSyntaxNode ParseParenElement();
BSyntaxNode ParseParen();
BSyntaxNode ParseAssignment();
BSyntaxNode ParseSum();
BSyntaxNode ParseNullCoalescing();
BSyntaxNode ParseTernary();
BSyntaxNode ParseShift();
BSyntaxNode ParseRel();
BSyntaxNode ParseEq();
BSyntaxNode ParseBAnd();
BSyntaxNode ParseExpression();
BSyntaxNode ParseXOr();
BSyntaxNode ParseBOr();
BSyntaxNode ParseLAnd();
BSyntaxNode ParseLOr();
BSyntaxNode ParseFactor();
BSyntaxNode ParseUnary();
BSyntaxNode ParseValue();
public:
BCParser(const std::vector<LexToken>& tokens);
BSyntaxNode ParseRoot();
};
}

513
src/node.cpp Normal file
View File

@@ -0,0 +1,513 @@
#include "bcrossc.hpp"
using namespace Tesses::Framework::Serialization::Json;
namespace Tesses::CrossLang {
BScopeNode::BScopeNode(bool isRoot,bool isExpression) : isRoot(isRoot), isExpression(isExpression)
{
}
bool BScopeNode::IsRoot()
{
return isRoot;
}
bool BScopeNode::IsExpression()
{
return isExpression;
}
TF_JToken BScopeNode::ToJsonObject()
{
JObject o;
o.SetValue("Type", "ScopeNode");
o.SetValue("IsRoot", isRoot);
o.SetValue("IsExpression", isExpression);
JArray a;
for(auto& item : this->childern)
{
a.Add(item->ToJsonObject());
}
o.SetValue("Childern",a);
return o;
}
bool BNull::IsExpression()
{
return true;
}
TF_JToken BNull::ToJsonObject()
{
return nullptr;
}
BVariableDeclarationNode::BVariableDeclarationNode(std::string documentation,std::string name, BType type, BSyntaxNode value)
: documentation(documentation), name(name), type(type), value(value)
{
}
bool BVariableDeclarationNode::IsExpression()
{
return false;
}
TF_JToken BVariableDeclarationNode::ToJsonObject()
{
JObject o;
o.SetValue("Type", "VariableDeclarationNode");
o.SetValue("TypeString", this->type.Stringify());
o.SetValue("Name", this->name);
o.SetValue("Documentation",this->documentation);
if(this->value)
o.SetValue("Value", this->value->ToJsonObject());
return o;
}
BBinaryExpression::BBinaryExpression(std::string_view type, BSyntaxNode left, BSyntaxNode right) : type(type), left(left), right(right)
{
}
bool BBinaryExpression::IsExpression()
{
return true;
}
TF_JToken BBinaryExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type",(std::string)this->type);
if(this->left)
o.SetValue("Left", this->left->ToJsonObject());
if(this->right)
o.SetValue("Right", this->right->ToJsonObject());
return o;
}
BUnaryExpression::BUnaryExpression(std::string_view type, BSyntaxNode value) : type(type), value(value)
{
}
bool BUnaryExpression::IsExpression()
{
return true;
}
TF_JToken BUnaryExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type",(std::string)this->type);
if(this->value)
o.SetValue("Value", this->value->ToJsonObject());
return o;
}
BTernaryExpression::BTernaryExpression(BSyntaxNode cond, BSyntaxNode yes, BSyntaxNode no): cond(cond), yes(yes), no(no)
{
}
bool BTernaryExpression::IsExpression()
{
return true;
}
TF_JToken BTernaryExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type","TernaryExpression");
if(this->cond)
o.SetValue("Condition", this->cond->ToJsonObject());
if(this->yes)
o.SetValue("True", this->yes->ToJsonObject());
if(this->no)
o.SetValue("False", this->no->ToJsonObject());
return o;
}
BIfStatement::BIfStatement(BSyntaxNode cond, BSyntaxNode yes, BSyntaxNode no): cond(cond), yes(yes), no(no)
{
}
bool BIfStatement::IsExpression()
{
return false;
}
TF_JToken BIfStatement::ToJsonObject()
{
JObject o;
o.SetValue("Type","IfStatement");
if(this->cond)
o.SetValue("Condition", this->cond->ToJsonObject());
if(this->yes)
o.SetValue("True", this->yes->ToJsonObject());
if(this->no)
o.SetValue("False", this->no->ToJsonObject());
return o;
}
BString::BString(std::string text) : text(text)
{}
bool BString::IsExpression()
{
return true;
}
TF_JToken BString::ToJsonObject()
{
return text;
}
BChar::BChar(char value) : value(value)
{}
bool BChar::IsExpression()
{
return true;
}
TF_JToken BChar::ToJsonObject()
{
JObject o;
o.SetValue("Type","Char");
o.SetValue("Value", std::string{value});
return o;
}
BLong::BLong(int64_t value) : value(value)
{}
bool BLong::IsExpression()
{
return true;
}
TF_JToken BLong::ToJsonObject()
{
return value;
}
BDouble::BDouble(double value) : value(value)
{}
bool BDouble::IsExpression()
{
return true;
}
TF_JToken BDouble::ToJsonObject()
{
return value;
}
BBoolean::BBoolean(bool value) : value(value)
{}
bool BBoolean::IsExpression()
{
return true;
}
TF_JToken BBoolean::ToJsonObject()
{
return value;
}
BArgumentNode::BArgumentNode(std::string name, BType type): name(name), type(type)
{}
bool BArgumentNode::IsExpression()
{
return false;
}
TF_JToken BArgumentNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","ArgumentNode");
o.SetValue("Name",this->name);
o.SetValue("TypeString",this->type.Stringify());
return o;
}
BGetVariableExpression::BGetVariableExpression(std::string name) : name(name)
{
}
bool BGetVariableExpression::IsExpression()
{
return true;
}
TF_JToken BGetVariableExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type","GetVariableExpression");
o.SetValue("Name",this->name);
return o;
}
bool BUndefined::IsExpression()
{
return true;
}
TF_JToken BUndefined::ToJsonObject()
{
JObject o;
o.SetValue("Type","Undefined");
return o;
}
BFunctionNode::BFunctionNode(std::string documentation,BType returnType, BType name, BSyntaxNode args, BSyntaxNode body): documentation(documentation), returnType(returnType), name(name), args(args), body(body)
{
}
bool BFunctionNode::IsExpression()
{
return false;
}
TF_JToken BFunctionNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","FunctionNode");
o.SetValue("ReturnType", this->returnType.Stringify());
o.SetValue("Name", this->name.Stringify());
o.SetValue("Documentation",this->documentation);
if(this->args)
o.SetValue("Arguments", this->args->ToJsonObject());
if(this->body)
o.SetValue("Body",this->body->ToJsonObject());
return o;
}
BClosureExpression::BClosureExpression(BType returnType, BSyntaxNode args, BSyntaxNode body): returnType(returnType), args(args), body(body)
{
}
bool BClosureExpression::IsExpression()
{
return true;
}
TF_JToken BClosureExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type","ClosureExpression");
o.SetValue("ReturnType", this->returnType.Stringify());
if(this->args)
o.SetValue("Arguments", this->args->ToJsonObject());
if(this->body)
o.SetValue("Body",this->body->ToJsonObject());
return o;
}
BDelegateNode::BDelegateNode(std::string documentation,BType returnType, BType name, BSyntaxNode args): documentation(documentation),returnType(returnType), name(name), args(args)
{
}
bool BDelegateNode::IsExpression()
{
return false;
}
TF_JToken BDelegateNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","DelegateNode");
o.SetValue("ReturnType", this->returnType.Stringify());
o.SetValue("Name", this->name.Stringify());
if(this->args)
o.SetValue("Arguments", this->args->ToJsonObject());
o.SetValue("Documentation",this->documentation);
return o;
}
BReturnNode::BReturnNode(BSyntaxNode retVal): retVal(retVal)
{
}
bool BReturnNode::IsExpression()
{
return false;
}
TF_JToken BReturnNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","ReturnNode");
if(this->retVal)
o.SetValue("Value",this->retVal->ToJsonObject());
return o;
}
BFunctionCallExpression::BFunctionCallExpression(BSyntaxNode fun, BSyntaxNode args, std::vector<BType> generics)
:fun(fun), args(args), generics(generics)
{
}
bool BFunctionCallExpression::IsExpression()
{
return true;
}
TF_JToken BFunctionCallExpression::ToJsonObject()
{
JObject o;
o.SetValue("Type","FunctionCallExpression");
if(this->args)
o.SetValue("Arguments",this->args->ToJsonObject());
if(this->fun)
o.SetValue("Function", this->fun->ToJsonObject());
if(!this->generics.empty())
{
JArray a;
for(auto& item : this->generics)
{
a.Add(item.Stringify());
}
o.SetValue("Generics",a);
}
return o;
}
BEachStatement::BEachStatement(BSyntaxNode var, BSyntaxNode list, BSyntaxNode body)
:var(var), list(list), body(body)
{
}
bool BEachStatement::IsExpression()
{
return false;
}
TF_JToken BEachStatement::ToJsonObject()
{
JObject o;
o.SetValue("Type","EachStatement");
if(this->var)
o.SetValue("Variable", this->var->ToJsonObject());
if(this->list)
o.SetValue("List",this->list->ToJsonObject());
if(this->body)
o.SetValue("Body", this->body->ToJsonObject());
return o;
}
BExternFuncNode::BExternFuncNode(std::string documentation,BType returnType, BType name, BSyntaxNode args): documentation(documentation),
returnType(returnType), name(name), args(args)
{
}
bool BExternFuncNode::IsExpression()
{
return false;
}
TF_JToken BExternFuncNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","ExternFuncNode");
if(this->args)
o.SetValue("Arguments",this->args->ToJsonObject());
o.SetValue("Name",this->name.Stringify());
o.SetValue("ReturnType",this->returnType.Stringify());
o.SetValue("Documentation",this->documentation);
return o;
}
BExternVariableNode::BExternVariableNode(std::string documentation,std::string name, BType type) : documentation(documentation), name(name), type(type)
{
}
bool BExternVariableNode::IsExpression()
{
return false;
}
TF_JToken BExternVariableNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","ExternVariableNode");
o.SetValue("Name",this->name);
o.SetValue("Documentation",this->documentation);
o.SetValue("TypeString", this->type.Stringify());
return o;
}
BNamespaceNode::BNamespaceNode(BType name, BSyntaxNode body):
name(name), body(body)
{
}
bool BNamespaceNode::IsExpression()
{
return false;
}
TF_JToken BNamespaceNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","NamespaceNode");
o.SetValue("Name",this->name.Stringify());
if(this->body)
o.SetValue("Body",this->body->ToJsonObject());
return o;
}
BUsingType::BUsingType(BType name, BType type):
name(name), type(type)
{
}
bool BUsingType::IsExpression()
{
return false;
}
TF_JToken BUsingType::ToJsonObject()
{
JObject o;
o.SetValue("Type","UsingType");
o.SetValue("Name",this->name.Stringify());
o.SetValue("TypeString",this->type.Stringify());
return o;
}
BUsingNamespace::BUsingNamespace(BType name): name(name)
{
}
bool BUsingNamespace::IsExpression()
{
return false;
}
TF_JToken BUsingNamespace::ToJsonObject()
{
JObject o;
o.SetValue("Type", "UsingNamespace");
o.SetValue("Name",this->name.Stringify());
return o;
}
BInterfaceNode::BInterfaceNode(std::string documentation, BType name):
documentation(documentation), name(name)
{
}
bool BInterfaceNode::IsExpression()
{
return false;
}
TF_JToken BInterfaceNode::ToJsonObject()
{
JObject o;
o.SetValue("Type","InterfaceNode");
o.SetValue("Documentation",this->documentation);
o.SetValue("Name",this->name.Stringify());
JArray a;
for(auto& item : this->inherits)
{
a.Add(item.Stringify());
}
o.SetValue("Inherits", a);
a.Clear();
for(auto& item : this->funs)
{
a.Add(item.ToJsonObject());
}
o.SetValue("Functions",a);
return o;
}
}

1087
src/parser.cpp Normal file

File diff suppressed because it is too large Load Diff