2-27-2025

This commit is contained in:
2025-02-27 04:31:52 -06:00
parent 2782f9af13
commit 0a87476cfa
7 changed files with 124 additions and 9 deletions

View File

@ -911,7 +911,7 @@ namespace Tesses::CrossLang
contscope = old_contscope;
brkscope = old_brkscope;
}
else if(adv.nodeName == CommaExpression && adv.nodeName.size() == 2)
else if(adv.nodeName == CommaExpression && adv.nodes.size() == 2)
{
GenNode(instructions,adv.nodes[0],scope,contscope,brkscope,contI,brkI);
GenPop(instructions,adv.nodes[0]);

View File

@ -165,6 +165,14 @@ namespace Tesses::CrossLang
node = AdvancedSyntaxNode::Create(GetVariableExpression, true,{"operator"+variable.text});
}
else if(IsIdentifier("new"))
{
if(i >= tokens.size()) throw std::out_of_range("End of file");
auto variable = tokens[i];
i++;
if(variable.type != LexTokenType::Identifier && variable.type != LexTokenType::Symbol) throw SyntaxException(variable.lineInfo, "Expected an identifier or a symbol got a " + LexTokenType_ToString(variable.type) + " \"" + variable.text + "\"");
node = AdvancedSyntaxNode::Create(GetFieldExpression, true, {AdvancedSyntaxNode::Create(GetVariableExpression,true,{"New"}), variable.text});
}
else if(IsIdentifier("embed"))
{
EnsureSymbol("(");
@ -339,6 +347,7 @@ namespace Tesses::CrossLang
{
return AdvancedSyntaxNode::Create(PrefixDecrementExpression,true,{ParseUnary()});
}
return ParseValue();
}
@ -557,6 +566,10 @@ namespace Tesses::CrossLang
}
return AdvancedSyntaxNode::Create(EachStatement,false,{item,list,body});
}
if(IsIdentifier("object"))
{
//TODO: complete this
}
if(IsIdentifier("enumerable"))
{
auto nameAndArgs = ParseExpression();

View File

@ -118,6 +118,18 @@ namespace Tesses::CrossLang
TVFSHeapObject* vfs;
return GetArgumentHeap(args,0,vfs);
}
static TObject New_Color(GCList& ls, std::vector<TObject> args)
{
int64_t r,g,b,a;
if(GetArgument(args,0,r) && GetArgument(args,1,g) && GetArgument(args,2,b))
{
if(!GetArgument(args,3,a)) {
a = 255;
}
return Tesses::Framework::Graphics::Color((uint8_t)r,(uint8_t)g,(uint8_t)b,(uint8_t)a);
}
return Tesses::Framework::Graphics::Colors::Black;
}
static TObject TypeOf(GCList& ls, std::vector<TObject> args)
{
if(args.size() < 1) return "Undefined";
@ -329,8 +341,10 @@ namespace Tesses::CrossLang
void TStd::RegisterRoot(GC* gc, TRootEnvironment* env)
{
GCList ls(gc);
env->permissions.canRegisterRoot=true;
TDictionary* newTypes = TDictionary::Create(ls);
env->DeclareFunction(gc, "ParseLong","Parse Long from String",{"arg","$base"},ParseLong);
env->DeclareFunction(gc, "ParseDouble","Parse Double from String",{"arg"},ParseDouble);
env->DeclareFunction(gc, "YieldEmumerable","Turn yield in function into enumerable",{"closure"},YieldEnumerableFunc);
@ -414,8 +428,10 @@ namespace Tesses::CrossLang
return Undefined();
});
env->DeclareFunction(gc,"ByteArray","Create bytearray, with optional either size (to size it) or string argument (to fill byte array)",{"$data"},ByteArray);
newTypes->DeclareFunction(gc,"Color","Create a new color",{"red","green","blue","$alpha"},New_Color);
gc->BarrierBegin();
env->DeclareVariable("InvokeMethod",MethodInvoker());
env->DeclareVariable("New", newTypes);
gc->BarrierEnd();
}
void TStd::RegisterStd(GC* gc, TRootEnvironment* env)

View File

@ -1,4 +1,3 @@
#include "CrossLang.hpp"
#include <iostream>
#include <fstream>
@ -6,6 +5,79 @@
namespace Tesses::CrossLang
{
static TObject AstToTObject(GCList& ls,SyntaxNode node)
{
if(std::holds_alternative<std::nullptr_t>(node))
return nullptr;
if(std::holds_alternative<int64_t>(node))
{
return std::get<int64_t>(node);
}
if(std::holds_alternative<double>(node))
{
return std::get<double>(node);
}
if(std::holds_alternative<bool>(node))
{
return std::get<bool>(node);
}
if(std::holds_alternative<std::string>(node))
{
std::string str = std::get<std::string>(node);
return str;
}
if(std::holds_alternative<char>(node))
{
char c = std::get<char>(node);
return c;
}
if(std::holds_alternative<Undefined>(node))
{
auto r = TDictionary::Create(ls);
ls.GetGC()->BarrierBegin();
r->SetValue("Type",std::string(UndefinedExpression));
ls.GetGC()->BarrierEnd();
return r;
}
if(std::holds_alternative<AdvancedSyntaxNode>(node))
{
auto asn = std::get<AdvancedSyntaxNode>(node);
auto r = TDictionary::Create(ls);
ls.GetGC()->BarrierBegin();
r->SetValue("Type",asn.nodeName);
r->SetValue("IsExpression",asn.isExpression);
TList* ls2 = TList::Create(ls);
for(auto item : asn.nodes)
{
ls2->Add(AstToTObject(ls,item));
}
r->SetValue("Arguments", ls2);
ls.GetGC()->BarrierEnd();
return r;
}
return nullptr;
}
static TObject VM_SourceToAst(GCList& ls, std::vector<TObject> args)
{
std::string str;
if(GetArgument(args,0,str))
{
std::stringstream strm(str);
std::vector<LexToken> tokens;
int res = Lex("memory.tcross",strm,tokens);
if(res != 0)
{
throw VMException("Lex error at line: " + std::to_string(res));
}
Parser p(tokens);
auto res2 =p.ParseRoot();
return AstToTObject(ls,res2);
}
return nullptr;
}
static TObject VM_Eval(GCList& ls, std::vector<TObject> args)
{
std::string str;
@ -201,7 +273,7 @@ namespace Tesses::CrossLang
dict->DeclareFunction(gc, "Eval", "Eval source code",{"source"}, VM_Eval);
dict->DeclareFunction(gc, "Compile", "Compile Source",{"dict"},VM_Compile);
dict->DeclareFunction(gc, "SourceToAst", "Convert source to ast", {"source"}, VM_SourceToAst);
gc->BarrierBegin();
env->DeclareVariable("VM", dict);

View File

@ -158,10 +158,9 @@ namespace Tesses::CrossLang {
}
return true;
}
else {
this->call_stack_entries.back()->Push(ls.GetGC(),Undefined());
}
}
this->call_stack_entries.back()->Push(ls.GetGC(),Undefined());
return false;
}
bool InterperterThread::Sub(GC* gc)