Still working on language

This commit is contained in:
Mike Nolan 2023-06-08 13:14:35 -05:00
parent c1a3acee07
commit a469aa8720
55 changed files with 1859 additions and 224 deletions

View File

@ -1,11 +1,18 @@
main = func($$args){
/*each(arg : args)
{
ls = create.array();
each(v : "GET /api/AddItem/https://www.youtube.com/watch?v=U1jjngDGd_Y HTTP/1.1\r\nHost: 192.168.0.112:3252\r\n\r\n")
each(v : "GET /api/AddItem/" + arg + "HTTP/1.1\r\nHost: 192.168.0.112:3252\r\n\r\n")
{
ls.add(v);
}
netstrm=net.tcpclient("192.168.0.112",3252);
netstrm.write(ls,ls.length);
netstrm.close();
}*/
console.readln();
}

View File

@ -5,8 +5,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="IL2C.Build" Version="0.4.113" />
<ItemGroup>
</ItemGroup>
<PropertyGroup>

View File

@ -1,7 +1,10 @@
using System.IO;
namespace tlang
{
internal class AddNode : Node
{
public Node Left {get;set;}
public Node Right {get;set;}
@ -35,18 +38,21 @@ namespace tlang
if(lNum != null && rNum != null)
return new TNumber(lNum.Value + rNum.Value);
return TObject.Number(lNum.Value + rNum.Value);
if(lStr != null && rStr != null)
return new TString(lStr.Value + rStr.Value);
return TObject.String(lStr.Value + rStr.Value);
if(lStr != null && rNum != null)
return new TString(lStr.Value + rNum.Value.ToString());
return TObject.String(lStr.Value + rNum.Value.ToString());
if(lNum != null && rStr != null)
return new TString(lNum.Value.ToString() + rStr.Value);
return TObject.String(lNum.Value.ToString() + rStr.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -45,7 +45,7 @@ namespace tlang
var nArg = arg as TNumber;
if(nArg != null && nArg.Value < str.Value.Length)
{
return new TChar(str.Value[(int)nArg.Value]);
return TObject.Char(str.Value[(int)nArg.Value]);
}
}
return TObject.Uninit;

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber((long)lnum.Value & (long)rnum.Value);
return TObject.Number((long)lnum.Value & (long)rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

37
tlanglib/BitwiseNot.cs Normal file
View File

@ -0,0 +1,37 @@
namespace tlang
{
public class BitwiseNot : Node
{
public Node Node {get;set;}
public BitwiseNot(Node node)
{
this.Node = node;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var l = Node.Execute(nodeEnv);
var lnum = l as TNumber;
var dleft = l as TDictionary;
if(dleft != null)
{
if(dleft.MemberExists("bnot"))
{
var mbm = dleft["bnot"] as ICallable;
if(mbm != null)
{
return mbm.Call();
}
}
}
if(lnum != null)
return TObject.Number(~(long)lnum.Value);
return TObject.Null;
}
}
}

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber((long)lnum.Value | (long)rnum.Value);
return TObject.Number((long)lnum.Value | (long)rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.IO;
namespace tlang
{
public class ClosureNode : Node
{
//j=func(a,b,c,d) 4
@ -18,5 +20,7 @@ namespace tlang
{
return new TInternalMethod(this,nodeEnv);
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value / rnum.Value);
return TObject.Number(lnum.Value / rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -22,16 +22,24 @@ namespace tlang
if(dict!= null)
{
var gIttrVar = dict["ittr"] as TDictionary;
var gIttrProp = dict["getIttr"] as ICallable;
var gIttrProp = dict["getittr"] as ICallable;
var myObj = gIttrVar;
if(gIttrProp != null)
{
var myObj= gIttrProp.Call() as TDictionary;
if(myObj != null)
var _myObj= gIttrProp.Call() as TDictionary;
if(_myObj != null)
{
myObj = _myObj;
}
}
if(myObj != null)
{
var reset = myObj["reset"] as ICallable;
var moveNext = myObj["movenext"] as ICallable;
var getCurrent = myObj["getCurrent"] as ICallable;
var getCurrent = myObj["getcurrent"] as ICallable;
if(reset != null) reset.Call();
if(moveNext != null)
while(moveNext.Call().AsBoolean)
@ -48,7 +56,8 @@ namespace tlang
}
}
}
}
else if(array != null)
{
@ -61,7 +70,7 @@ namespace tlang
{
foreach(var c in str.Value)
{
yield return new TChar(c);
yield return TObject.Char(c);
}
}
@ -73,8 +82,9 @@ namespace tlang
var ls = List.Execute(env);
bool isRunning=true;
env["last_ittr"]= new TExternalMethod((args)=>{
isRunning=false;
return new TNumber(1);
return TObject.True;
});
TObject last = TObject.Uninit;

View File

@ -14,7 +14,7 @@ namespace tlang
{
if(node is EnvironmentNode)
{
returnType =node.Execute(nodeEnv.SubEnv);
returnType = node.Execute(nodeEnv.SubEnv);
}else{
returnType = node.Execute(nodeEnv);
}

View File

@ -17,6 +17,12 @@ namespace tlang
var r = Right.Execute(nodeEnv);
var lnum = l as TNumber;
var rnum = r as TNumber;
var lstr = l as TString;
var rstr = r as TString;
var lbool = l as TBoolean;
var rbool = r as TBoolean;
var lchar = l as TChar;
var rchar = r as TChar;
var dleft = l as TDictionary;
if(dleft != null)
@ -28,13 +34,34 @@ namespace tlang
{
return mbm.Call(r);
}
else {
var dright = r as TDictionary;
if(dright != null)
{
return TObject.Boolean(dleft == dright);
}
}
}
else {
var dright = r as TDictionary;
if(dright != null)
{
return TObject.Boolean(dleft == dright);
}
}
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value == rnum.Value ? 1 : 0);
return TObject.Boolean(lnum.Value == rnum.Value);
if(lbool != null && rbool != null)
return TObject.Boolean(lbool.Value == rbool.Value);
if(lstr != null && rstr != null)
return TObject.Boolean(lstr.Value == rstr.Value);
if(lchar != null && rchar != null)
return TObject.Boolean(lchar.Value == rchar.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -20,11 +20,12 @@ namespace tlang
var env = nodeEnv.SubEnv;
bool isRunning=true;
env["last_ittr"]= new TExternalMethod((args)=>{
isRunning=false;
return new TNumber(1);
return TObject.True;
});
Init.Execute(env);
TObject obj=new TUninit();
TObject obj= TObject.Uninit;
while(Condition.Execute(env).AsBoolean && isRunning)
{
obj=Body.Execute(env);

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace tlang
@ -22,7 +23,9 @@ namespace tlang
return n.Call(Args.Select<Node,TObject>(e=>e.Execute(nodeEnv)).ToArray());
}
return new TUninit();
return TObject.Uninit;
}
}
}
}

View File

@ -16,6 +16,7 @@ namespace tlang
Name.SetValue(nodeEnv,res);
return res;
}
}
public class GetVariableValue : Node
@ -35,5 +36,7 @@ namespace tlang
{
nodeEnv[Name] = value;
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value >= rnum.Value ? 1 : 0);
return TObject.Boolean(lnum.Value >= rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value > rnum.Value ? 1 : 0);
return TObject.Boolean(lnum.Value > rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -17,11 +17,6 @@ namespace tlang
{
var condition = Condition.Execute(nodeEnv);
return condition.AsBoolean ? Yes.Execute(nodeEnv) : No.Execute(nodeEnv);
}
}

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber((long)lnum.Value << (int)rnum.Value);
return TObject.Number((long)lnum.Value << (int)rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value <= rnum.Value ? 1 : 0);
return TObject.Boolean(lnum.Value <= rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value < rnum.Value ? 1 : 0);
return TObject.Boolean(lnum.Value < rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -205,9 +205,21 @@ namespace tlang
case ':':
case ',':
case ';':
case '=':
FlushBuilder();
FlushBuilder();
_tokens.Add(LexToken.FromGeneralToken(read));
break;
case '=':
FlushBuilder();
if(next == read)
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
break;
case ' ':
case '\n':

View File

@ -25,7 +25,7 @@ namespace tlang
return lor.Call(r);
}
}
return new TNumber(l.AsBoolean && r.AsBoolean ? 1 : 0);
return TObject.Boolean(l.AsBoolean && r.AsBoolean);
}
}
}

View File

@ -25,7 +25,7 @@ namespace tlang
return lor.Call(r);
}
}
return new TNumber(l.AsBoolean || r.AsBoolean ? 1 : 0);
return TObject.Boolean(l.AsBoolean || r.AsBoolean);
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace tlang
{
@ -17,6 +18,7 @@ namespace tlang
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var res=Parent.Execute(nodeEnv);
@ -25,6 +27,12 @@ namespace tlang
var str = res as TString;
var chr = res as TChar;
var array = res as TArray;
var myBool = res as TBoolean;
if(Text == "toChar2")
{
throw new Exception(res.GetType().Name);
}
if(dict != null)
{
var n = dict[Text] as ICallable;
@ -32,35 +40,53 @@ namespace tlang
{
return n.Call(Args.Select<Node,TObject>(e=>e.Execute(nodeEnv)).ToArray());
}
}else if(chr != null)
}else if(myBool != null)
{
if(Text == "toUpper")
//branch
if(Text == "toChar")
{
return new TChar(char.ToUpper(chr.Value));
}
if(Text == "toLower")
{
return new TChar(char.ToLower(chr.Value));
}
if(Text == "toString")
{
return new TString(chr.Value.ToString());
return TObject.Char(myBool.Value ? 'y' : 'n');
}
if(Text == "toNumber")
{
return new TNumber(chr.Value);
return TObject.Number(myBool.Value ? 1 : 0);
}
}
else if(chr != null)
{
if(Text == "toUpper")
{
return TObject.Char(char.ToUpper(chr.Value));
}
if(Text == "toLower")
{
return TObject.Char(char.ToLower(chr.Value));
}
if(Text == "toNumber")
{
return TObject.Number(chr.Value);
}
}
else if(array != null)
{
if(Text == "add")
{
if(Args.Count == 1)
{
TObject obj = Args[0].Execute(nodeEnv);
array.Items.Add(obj);
return obj;
return res;
}
}
if(Text == "remove")
{
if(Args.Count == 1)
{
TObject obj = Args[0].Execute(nodeEnv);
array.Items.Remove(obj);
return res;
}
}
if(Text == "at")
@ -97,33 +123,62 @@ namespace tlang
}
return value;
}
if(Text == "length" || Text == "count")
{
return new TNumber(array.Items.Count);
}
}
else if(integer != null)
{
if(Text == "toString")
if(Text == "int")
{
return new TString(integer.Value.ToString());
return TObject.Number(Math.Round(integer.Value));
}
if(Text == "abs")
{
return new TNumber(Math.Abs(integer.Value));
return TObject.Number(Math.Abs(integer.Value));
}
if(Text == "toChar")
{
return new TChar((char)integer.Value);
return TObject.Char((char)integer.Value);
}
}else if(str != null)
{
if(Text == "toArray")
{
var _array=TObject.Array();
foreach(var c in str.Value)
{
_array.Items.Add(TObject.Char(c));
}
}
if(Text == "replace")
{
//todo fill this method out
if(Args.Count >= 2)
return TObject.String(str.Value.Replace(Args[0].Execute(nodeEnv).ToString(),Args[1].Execute(nodeEnv).ToString()));
}
if(Text == "split")
{
if(Args.Count >= 1)
{
var text= str.Value;
var splitOn = Args[0].Execute(nodeEnv).ToString();
int count = int.MaxValue;
bool splitOp = false;
if(Args.Count >= 2)
{
splitOp = Args[1].Execute(nodeEnv).AsBoolean;
}
if(Args.Count >= 3)
{
var n = Args[2].Execute(nodeEnv) as TNumber;
if(n != null) count = (int)n.Value;
}
var array1=TObject.Array();
array1.Items.AddRange(text.Split(new string[]{splitOn},count,splitOp ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None).Select<string,TString>(e=>TObject.String(e)));
return array1;
}
//todo fill this method out
//str.Value.Split(,,StringSplitOptions.TrimEntries);
}
@ -131,28 +186,36 @@ namespace tlang
{
string val = " ";
if(Args.Count > 0){
var firstArg = Args[0].Execute(nodeEnv);
var c = firstArg as TChar;
var mystr = firstArg as TString;
var mynum = firstArg as TNumber;
if(c!=null) val = c.Value.ToString();
if(mystr != null) val = mystr.Value.ToString();
if(mynum != null) val = mynum.Value.ToString();
val = Args[0].Execute(nodeEnv).ToString();
}
return new TNumber(str.Value.StartsWith(val) ? 1 : 0);
return TObject.Boolean(str.Value.StartsWith(val));
}
if(Text == "endsWith")
{
string val = " ";
if(Args.Count > 0){
val = Args[0].Execute(nodeEnv).ToString();
}
return TObject.Boolean(str.Value.EndsWith(val));
}
if(Text == "trimStart")
{
if(Args.Count == 0)
{
return new TString(str.Value.TrimStart());
return TObject.String(str.Value.TrimStart());
}else{
return new TString(str.Value.TrimStart(Args.Select<Node,char>(n=>{
return TObject.String(str.Value.TrimStart(Args.Select<Node,char>(n=>{
var nV=n.Execute(nodeEnv) as TChar;
if(nV != null)
{
@ -167,10 +230,10 @@ namespace tlang
//trimend()
if(Args.Count == 0)
{
return new TString(str.Value.TrimEnd());
return TObject.String(str.Value.TrimEnd());
}else{
return new TString(str.Value.TrimEnd(Args.Select<Node,char>(n=>{
return TObject.String(str.Value.TrimEnd(Args.Select<Node,char>(n=>{
var nV=n.Execute(nodeEnv) as TChar;
if(nV != null)
{
@ -180,19 +243,28 @@ namespace tlang
}).ToArray()));
}
}
if(Text == "toString")
{
return str;
}
}
if(Text == "toString") return new TString("");
if(dict == null)
{
if(Text == "toString")
{
return TObject.String(res.ToString());
}
if(Text == "toBool")
{
return TObject.Boolean(res.AsBoolean);
}
}
if(Text == "toString") return TObject.String("");
return new TUninit();
return TObject.Uninit;
}
}
}
}

View File

@ -42,9 +42,26 @@ namespace tlang
{
if(Name == "length" || Name == "count")
{
return new TNumber(array.Items.Count);
return TObject.Number(array.Items.Count);
}
if(Name == "ittr")
{
var ittr = TObject.Dictionary();
var enumerator=array.Items.GetEnumerator();
ittr["getcurrent"] = new TExternalMethod(()=>{
return enumerator.Current;
});
ittr["movenext"] = new TExternalMethod(()=>{
return TObject.Boolean(enumerator.MoveNext());
});
ittr["reset"] = new TExternalMethod(()=>{
});
ittr["dispose"] = new TExternalMethod(()=>{
enumerator.Dispose();
});
return ittr;
}
}
if(dict != null)
{
@ -66,11 +83,11 @@ namespace tlang
{
if(Name == "abs")
{
return new TNumber(Math.Abs(integer.Value));
return TObject.Number(Math.Abs(integer.Value));
}
if(Name == "int")
{
return new TNumber(Math.Round(integer.Value));
return TObject.Number(Math.Round(integer.Value));
}
}
if(str != null)
@ -78,24 +95,44 @@ namespace tlang
if(Name == "number")
{
double value;
if(double.TryParse(str.Value,out value))
{
return TObject.Number(value);
}
return TObject.Null;
}
if(Name == "ittr")
{
var ittr = TObject.Dictionary();
var enumerator=str.Value.GetEnumerator();
ittr["getcurrent"] = new TExternalMethod(()=>{
return TObject.Char(enumerator.Current);
});
ittr["movenext"] = new TExternalMethod(()=>{
return TObject.Boolean(enumerator.MoveNext());
});
ittr["reset"] = new TExternalMethod(()=>{
enumerator.Reset();
});
ittr["dispose"] = new TExternalMethod(()=>{
enumerator.Dispose();
});
return ittr;
}
if(Name == "boolean")
{
return TObject.Boolean(str.Value.ToLower() == "true");
}
if(Name == "length" || Name == "count")
{
return new TNumber(str.Value.Length);
}
if(Name == "list")
{
return TObject.Number(str.Value.Length);
}
}
return new TUninit();
return TObject.Uninit;
}
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value % rnum.Value);
return TObject.Number(lnum.Value % rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value * rnum.Value);
return TObject.Number(lnum.Value * rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

38
tlanglib/NegativeNode.cs Normal file
View File

@ -0,0 +1,38 @@
namespace tlang
{
public class NegativeNode : Node
{
public Node Node {get;set;}
public NegativeNode(Node node)
{
this.Node = node;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var l = Node.Execute(nodeEnv);
var lnum = l as TNumber;
var dleft = l as TDictionary;
if(dleft != null)
{
if(dleft.MemberExists("neg"))
{
var mbm = dleft["neg"] as ICallable;
if(mbm != null)
{
return mbm.Call();
}
}
}
if(lnum != null)
return TObject.Number(-lnum.Value);
return TObject.Null;
}
}
}

View File

@ -1,7 +1,11 @@
using System;
using System.IO;
namespace tlang
{
public abstract class Node
{
public abstract TObject Execute(IScopeEnvironment nodeEnv);
}
}

37
tlanglib/Not.cs Normal file
View File

@ -0,0 +1,37 @@
namespace tlang
{
public class NotNode : Node
{
public Node Node {get;set;}
public NotNode(Node node)
{
this.Node = node;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var l = Node.Execute(nodeEnv);
var dleft = l as TDictionary;
if(dleft != null)
{
if(dleft.MemberExists("not"))
{
var mbm = dleft["not"] as ICallable;
if(mbm != null)
{
return mbm.Call();
}
}
}
return TObject.Boolean(!l.AsBoolean);
}
}
}

View File

@ -15,8 +15,14 @@ namespace tlang
{
var l = Left.Execute(nodeEnv);
var r = Right.Execute(nodeEnv);
var lnum = l as TNumber;
var lnum = l as TNumber;
var rnum = r as TNumber;
var lstr = l as TString;
var rstr = r as TString;
var lbool = l as TBoolean;
var rbool = r as TBoolean;
var lchar = l as TChar;
var rchar = r as TChar;
var dleft = l as TDictionary;
if(dleft != null)
@ -27,14 +33,31 @@ namespace tlang
if(mbm != null)
{
return mbm.Call(r);
}else {
var dright = r as TDictionary;
if(dright != null)
{
return TObject.Boolean(dleft != dright);
}
}
} else {
var dright = r as TDictionary;
if(dright != null)
{
return TObject.Boolean(dleft != dright);
}
}
}
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value != rnum.Value ? 1 : 0);
return new TNull();
return TObject.Boolean(lnum.Value != rnum.Value);
if(lbool != null && rbool != null)
return TObject.Boolean(lbool.Value != rbool.Value);
if(lstr != null && rstr != null)
return TObject.Boolean(lstr.Value != rstr.Value);
if(lchar != null && rchar != null)
return TObject.Boolean(lchar.Value != rchar.Value);
return TObject.Null;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@ namespace tlang
var obj=Node.Execute(nodeEnv);
var newVal=new SubtractNode(new ConstNode(obj),new ConstNode(new TNumber(1))).Execute(nodeEnv);
var newVal=new SubtractNode(new ConstNode(obj),new ConstNode(TObject.Number(1))).Execute(nodeEnv);
Node.SetValue(nodeEnv,newVal);
@ -23,4 +23,4 @@ namespace tlang
return obj;
}
}
}
}

View File

@ -14,7 +14,7 @@ namespace tlang
var obj=Node.Execute(nodeEnv);
var newVal=new AddNode(new ConstNode(obj),new ConstNode(new TNumber(1))).Execute(nodeEnv);
var newVal=new AddNode(new ConstNode(obj),new ConstNode(TObject.Number(1))).Execute(nodeEnv);
Node.SetValue(nodeEnv,newVal);

View File

@ -30,10 +30,10 @@ public class ApplicationRunner
List<TString> args2=new List<TString>();
foreach(var argument in args)
{
args2.Add(new TString(argument));
args2.Add(TObject.String(argument));
}
var res= parser.Execute(env,args2.ToArray()) as TNumber;
var res= parser.Execute(env/*,args2.ToArray()*/) as TNumber;

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber((long)lnum.Value >> (int)rnum.Value);
return TObject.Number((long)lnum.Value >> (int)rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -9,12 +9,14 @@ namespace tlang
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var sub = First ? nodeEnv : nodeEnv.SubEnv;
TObject obj=new TNull();
TObject obj=TObject.Null;
foreach(var item in Body)
{
obj=item.Execute(sub);
}
return obj;
}
}
}

View File

@ -32,9 +32,11 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber(lnum.Value - rnum.Value);
return TObject.Number(lnum.Value - rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -1,15 +1,70 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TArray : TObject
{
public override bool AsBoolean => Items.Count > 0;
public TArray()
public override JToken AsToken()
{
var ls = new JArray();
foreach(var item in Items)
{
ls.Add(item.AsToken());
}
return ls;
}
public byte[] ToBytes()
{
byte[] data = new byte[Items.Count];
SetBytes(data,0,data.Length);
return data;
}
public void SetBytes(byte[] data,int offset,int count)
{
count = Math.Min(data.Length - offset,count);
for(int i = 0;i<count;i++)
{
var item = Items[i+offset];
var c = item as TChar;
var n = item as TNumber;
if(c != null) data[i] = (byte)c.Value;
if(n != null) data[i] = (byte)n.Value;
}
}
public static TArray FromBytes(byte[] data,int offset,int count)
{
var array = TObject.Array(count);
for(int i = 0;i<count;i++)
{
array.Items[i+offset] = TObject.Number(data[i]);
}
return array;
}
public override bool AsBoolean => Items.Count > 0;
public TArray(int sz=0)
{
Items =new List<TObject>();
for(int i = 0;i<sz;i++)
{
Items.Add(TObject.Uninit);
}
}
public override string ToString()
{
StringBuilder b = new StringBuilder();
b.AppendLine("Array Items:");
foreach(var item in Items)
{
b.AppendLine(item.ToString());
}
return b.ToString();
}
public List<TObject> Items {get;set;}=new List<TObject>();
public List<TObject> Items {get;set;}
}
}

24
tlanglib/TBoolean.cs Normal file
View File

@ -0,0 +1,24 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TBoolean : TObject
{
public override JToken AsToken()
{
return JValue.FromObject(Value);
}
public bool Value {get;set;}
public override bool AsBoolean => Value;
public TBoolean(bool value)
{
Value = value;
}
public override string ToString()
{
return Value ? "true" : "false";
}
}
}

View File

@ -1,7 +1,17 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
internal class TChar : TObject
public class TChar : TObject
{
public override JToken AsToken()
{
return JValue.FromObject(Value);
}
public static implicit operator char(TChar c)
{
return c.Value;
}
public override bool AsBoolean => Value != '\0';
public char Value {get;set;}
public TChar(char v)

View File

@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TRootDict : TDictionary
{
IScopeEnvironment env;
public TRootDict(IScopeEnvironment env)
{
@ -24,6 +28,19 @@ namespace tlang
}
public class TDictionary : TObject
{
public override JToken AsToken()
{
if(this.TryGetTimeSpan(out var ts))
{
return JValue.FromObject(ts);
}
JObject obj=new JObject();
foreach(var item in this.items)
{
obj.Add(item.Key,item.Value.AsToken());
}
return obj;
}
public override bool AsBoolean {get =>true;}
public TDictionary()
{
@ -53,11 +70,51 @@ namespace tlang
items.Add(name,obj);
}
}
public override string ToString()
{
if(items.ContainsKey("toString"))
{
var tostr = items["toString"] as ICallable;
if(tostr != null)
{
var res= tostr.Call().ToString();
return res;
}
}
StringBuilder b = new StringBuilder();
b.AppendLine("Dictionary Items:");
foreach(var item in items)
{
b.AppendLine($"{item.Key}={item.Value.ToString()}");
}
return b.ToString();
}
public virtual bool MemberExists(string name)
{
return items.ContainsKey(name);
}
internal IEnumerable<TDictionary> GetMembers()
{
foreach(var item in items)
{
TDictionary dict = TObject.Dictionary();
dict["getname"] = new TExternalMethod(()=>{
return TObject.String(item.Key);
});
dict["getvalue"] = new TExternalMethod(()=>{
return item.Value;
});
dict["setvalue"] = new TExternalMethod((args)=>{
if(args.Length == 1)
{
items[item.Key] = args[0];
}
});
yield return dict;
}
}
public TObject this[string variable] { get => GetMember(variable); set => SetMember(variable,value); }

View File

@ -1,7 +1,13 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TInternalObject : TObject
{
public override JToken AsToken()
{
return JValue.FromObject((object)null);
}
public override bool AsBoolean => true;
public TInternalObject(object val)
{

View File

@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace tlang {
public class TLangGenerator
{
Lazy<(List<LexToken> Tokens,string Source)> srcCode;
public TLangGenerator(string src)
{
srcCode=new Lazy<(List<LexToken> Tokens,string Source)>(()=>{
string text = Lex(src);
Lexer lexer=new Lexer(new StringReader(text));
return (lexer.Tokens,text);
});
}
public string SourceCode
{
get => srcCode.Value.Source;
}
public string Execute(Action<RootEnvironment> parser)
{
Parser p = new Parser(srcCode.Value.Tokens);
RootEnvironment env=new RootEnvironment();
p.LoadEnvironment(env);
StringBuilder builder=new StringBuilder();
env.SetVariable("__writer",new TExternalMethod((args)=>{
if(args.Length >= 1)
{
foreach(var arg in args)
{
builder.Append(arg.ToString());
}
}
return TObject.Null;
}));
parser?.Invoke(env);
p.Execute(env);
//
return builder.ToString();
}
private string Lex(string text)
{
StringBuilder tlangFile = new StringBuilder();
StringBuilder b=new StringBuilder();
bool lastInCode=true;
bool inCode=false;
int i = 0;
bool isDone = false;
Action<int> appendChar = (c)=>{
if((lastInCode != inCode || isDone) && b.Length > 0)
{
if(inCode ^ (isDone && !inCode))
{
tlangFile.Append("__writer(\"");
b.Replace("\n","\\n").Replace("\t","\\t").Replace("\r","\\r").Replace("\"","\\\"").Replace("\'","\\\'");
foreach(var _c in b.ToString())
{
if(_c < 32 || _c > 126)
{
tlangFile.Append($"\\x{((int)_c).ToString("X2")}");
}else{
tlangFile.Append(_c);
}
}
b.Clear();
tlangFile.Append("\");");
}
if(!inCode ^ (isDone && inCode))
{
if(b.Length > 0)
{
tlangFile.Append(b);
}
b.Clear();
}
}
if(c != -1)
{
b.Append((char)c);
}
lastInCode = inCode;
};
bool escapedByAt = false;
while(i < text.Length)
{
char curChar = text[i++];
int nextChar = i<text.Length ? text[i] : -1;
switch(curChar)
{
case '@':
if(nextChar == '{')
{
appendChar('{');
i++;
}else if(nextChar == '}') {
appendChar('}');
i++;
}else if(nextChar == '@')
{
appendChar('@');
i++;
}else{
if(!escapedByAt)
{
inCode=true;
escapedByAt=true;
//we are a variable
foreach(var c in "__writer(")
appendChar(c);
}else
if(escapedByAt)
{
appendChar(')');
appendChar(';');
inCode = false;
escapedByAt = false;
}
}
break;
case '{':
if(nextChar == '{')
{
i++;
inCode = !inCode;
}else{
appendChar(curChar);
}
break;
case '}':
if(nextChar == '}')
{
i++;
inCode = !inCode;
}else{
appendChar(curChar);
}
break;
default:
appendChar(curChar);
break;
}
}
isDone=true;
appendChar(-1);
return tlangFile.ToString();
}
}
}

View File

@ -1,9 +1,14 @@
using System;
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TInternalMethod : TObject, ICallable
{
public override JToken AsToken()
{
return new JValue((object)null);
}
public override bool AsBoolean => true;
public TInternalMethod(ClosureNode node,IScopeEnvironment env)
{
@ -28,7 +33,7 @@ namespace tlang
}
if(i==Body.Arguments.Count-1)
{
var tarray = new TArray();
var tarray = TObject.Array();
env[Body.Arguments[i].TrimStart('$')] =tarray;
for(int j = i;j<args.Length;j++)
{
@ -42,6 +47,7 @@ namespace tlang
}
private int optionalArgs(int argLen)
{
for(int i =0;i<Body.Arguments.Count;i++)
{
if(Body.Arguments[i].StartsWith("$$"))
@ -66,12 +72,37 @@ namespace tlang
}
public class TExternalMethod : TObject,ICallable
{
public override JToken AsToken()
{
return new JValue((object)null);
}
Func<TObject[],TObject> cb;
public TExternalMethod(Func<TObject[],TObject> func)
{
cb = func;
}
public TExternalMethod(Action<TObject[]> func)
{
cb = (args)=>{
func(args);
return Uninit;
};
}
public TExternalMethod(Func<TObject> func)
{
cb = (args)=>{
return func();
};
}
public TExternalMethod(Action func)
{
cb = (args)=>{
func();
return Uninit;
};
}
public override bool AsBoolean => true;
public TObject Call(params TObject[] args)

View File

@ -1,3 +1,5 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TNull : TObject
@ -7,5 +9,9 @@ namespace tlang
{
return "null";
}
public override JToken AsToken()
{
return JValue.CreateNull();
}
}
}

View File

@ -1,7 +1,49 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TNumber : TObject
{
public static implicit operator double(TNumber n)
{
return n.Value;
}
public static explicit operator long(TNumber n)
{
return (long)n.Value;
}
public static explicit operator float(TNumber n)
{
return (float)n.Value;
}
public static explicit operator int(TNumber n)
{
return (int)n.Value;
}
public static explicit operator short(TNumber n)
{
return (short)n.Value;
}
public static explicit operator ushort(TNumber n)
{
return (ushort)n.Value;
}
public static explicit operator uint(TNumber n)
{
return (uint)n.Value;
}
public static explicit operator ulong(TNumber n)
{
return (ulong)n.Value;
}
public static explicit operator byte(TNumber n)
{
return (byte)n.Value;
}
public static explicit operator sbyte(TNumber n)
{
return (sbyte)n.Value;
}
public double Value {get;set;}
public override bool AsBoolean => Value != 0;
@ -14,6 +56,10 @@ namespace tlang
{
return Value.ToString();
}
public override JToken AsToken()
{
return new JValue(Value);
}
}

View File

@ -1,9 +1,261 @@
using System;
using System.IO;
using Newtonsoft.Json.Linq;
namespace tlang
{
public static class TObjectExtensions
{
public static bool TryGetFieldOrProperty(this TObject obj,string key,out TObject val)
{
val=TObject.Uninit;
var dict = obj as TDictionary;
if(dict != null)
{
if(dict.MemberExists($"get{key}"))
{
var p= dict[$"get{key}"] as ICallable;
if(p != null)
{
val = p.Call();
return true;
}
}
if(dict.MemberExists(key))
{
val = dict[key];
return true;
}
}
return false;
}
public static string GetExtObjType(this TObject obj)
{
var dict = obj as TDictionary;
if(dict != null)
{
if(!dict.MemberExists("_extobjtype")) return "";
var _extobjtype=dict["_extobjtype"] as TString;
if(_extobjtype != null) return _extobjtype.Value;
}
return "";
}
public static TDictionary ToTObject(this TimeSpan ts)
{
var dict = TObject.Dictionary();
dict["_extobjtype"] ="timespan";
dict["h"] = ts.Hours;
dict["m"] = ts.Minutes;
dict["s"] = ts.Seconds;
dict["totalseconds"] = ts.TotalSeconds;
return dict;
}
public static bool TryGetTimeSpan(this TObject obj,out TimeSpan ts)
{
ts=TimeSpan.Zero;
var dict = obj as TDictionary;
if(dict != null)
{
if(dict.GetExtObjType() != "timespan") return false;
if(obj.TryGetFieldOrProperty("totalseconds",out var v))
{
var _totalSecond = v as TNumber;
double totalSeconds = 0;
if(_totalSecond != null) totalSeconds = _totalSecond.Value;
ts = TimeSpan.FromSeconds(totalSeconds);
return true;
}
if(obj.TryGetFieldOrProperty("h",out var h) && obj.TryGetFieldOrProperty("m",out var m) && obj.TryGetFieldOrProperty("s",out var s))
{
var _h = h as TNumber;
var _m = m as TNumber;
var _s = s as TNumber;
int __h=0;
int __m=0;
int __s=0;
if(_h != null) __h = (int)_h.Value;
if(_m != null) __m = (int)_m.Value;
if(_s != null) __s = (int)_s.Value;
ts=new TimeSpan(__h,__m,__s);
return true;
}
}
return false;
}
}
public abstract class TObject
{
public static explicit operator bool(TObject obj)
{
return obj.AsBoolean;
}
public static implicit operator TObject(double dbl)
{
return Number(dbl);
}
public static implicit operator TObject(Func<TObject[],TObject> o)
{
return new TExternalMethod(o);
}
public static implicit operator TObject(string str)
{
return String(str);
}
public static implicit operator TObject(bool b)
{
return Boolean(b);
}
public static implicit operator TObject(int n)
{
return Number(n);
}
public static implicit operator TObject(long n)
{
return Number(n);
}
public static implicit operator TObject(short n)
{
return Number(n);
}
public static implicit operator TObject(TimeSpan ts)
{
return ts.ToTObject();
}
public static implicit operator TObject(float f)
{
return Number(f);
}
public static implicit operator TObject(ushort s)
{
return Number(s);
}
public static implicit operator TObject(uint s)
{
return Number(s);
}
public static implicit operator TObject(ulong l)
{
return Number(l);
}
public static implicit operator TObject(char c)
{
return Char(c);
}
public static implicit operator TObject(Stream strm)
{
return Parser.CreateStream(strm);
}
public static implicit operator TObject(TObject[] array)
{
var a=Array();
a.Items.AddRange(array);
return a;
}
public static implicit operator TObject(byte b)
{
return Number(b);
}
public static implicit operator TObject(sbyte b)
{
return Number(b);
}
public static TObject FromToken(JToken token)
{
var obj = token as JObject;
var val = token as JValue;
var arr = token as JArray;
if(obj != null)
{
var dict = Dictionary();
foreach(var item in obj)
{
dict.SetMember(item.Key,FromToken(item.Value));
}
return dict;
}
if(arr != null)
{
var array = Array();
foreach(var item in arr)
{
array.Items.Add(FromToken(item));
}
return array;
}
if(val != null)
{
if(val.Type == JTokenType.Integer)
{
return Number(val.ToObject<long>());
}
if(val.Type == JTokenType.Float)
{
return Number(val.ToObject<double>());
}
if(val.Type == JTokenType.String)
{
return String(val.ToObject<string>());
}
if(val.Type == JTokenType.Null)
{
return Null;
}
if(val.Type == JTokenType.Undefined)
{
return Uninit;
}
if(val.Type == JTokenType.TimeSpan)
{
//h:m:s
var tdict=Dictionary();
tdict["_extobjtype"] = String("timespan");
}
if(val.Type == JTokenType.Boolean)
{
return TObject.Boolean(val.ToObject<bool>());
}
}
return Uninit;
}
public abstract JToken AsToken();
public override string ToString()
{
return "TObject";
}
public abstract bool AsBoolean {get;}
public static TObject Uninit => new TUninit();
public static TObject Null => new TNull();
public static TObject True => new TBoolean(true);
public static TObject False => new TBoolean(false);
public static TChar Char(char c)
{
return new TChar(c);
}
public static TNumber Number(double dbl)
{
return new TNumber(dbl);
}
public static TString String(string text)
{
return new TString(text);
}
public static TDictionary Dictionary()
{
return new TDictionary();
}
public static TArray Array(int bufferSize=0)
{
return new TArray(bufferSize);
}
public static TObject Boolean(bool v)
{
return new TBoolean(v);
}
}
}

View File

@ -1,7 +1,13 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TString : TObject
{
public static implicit operator string(TString s)
{
return s.Value;
}
public string Value {get;set;}
public override bool AsBoolean => Value.Length > 0;
@ -14,5 +20,10 @@ namespace tlang
{
return Value;
}
public override JToken AsToken()
{
return JValue.FromObject(Value);
}
}
}

View File

@ -1,8 +1,16 @@
using Newtonsoft.Json.Linq;
namespace tlang
{
public class TUninit : TObject
{
public override bool AsBoolean {get=>false;}
public override JToken AsToken()
{
return JValue.CreateUndefined();
}
public override string ToString()
{
return "undefined";

View File

@ -19,20 +19,27 @@ namespace tlang
var env = nodeEnv.SubEnv;
env["last_ittr"]= new TExternalMethod((args)=>{
if(args.Length > 0)
{
return TObject.Boolean(!args[0].AsBoolean || !isRunning);
}
isRunning=false;
return new TNumber(1);
return TObject.True;
});
TObject obj=new TUninit();
TObject obj=TObject.Uninit;
if(!DoLoop) isRunning = Condition.Execute(env).AsBoolean;
while(isRunning)
{
obj=Body.Execute(env);
if(isRunning)
isRunning = Condition.Execute(env).AsBoolean;
}
return obj;
}
}
}

View File

@ -32,9 +32,9 @@ namespace tlang
}
if(lnum != null && rnum != null)
return new TNumber((long)lnum.Value ^ (long)rnum.Value);
return TObject.Number((long)lnum.Value ^ (long)rnum.Value);
return new TNull();
return TObject.Null;
}
}
}

View File

@ -1,7 +1,35 @@
main = func($$args){
____load = func(file)
{
___code_txt="";
if(file.endsWith(".txt"))
{
each(fs.readalltext(file).split('\n'))
{
___code_txt = ___code_txt + ____load(fs.relative_to_parent_of(file,item)) + "\n";
}
}else{
if(!fs.file_exists(file)) console.writeln("WARN: " + file + " Does not exist");
___code_txt=fs.readalltext(file);
}
___code_txt;
}
if(fs.file_exists(args[0]))
{
code = reflection.parse_code(fs.readalltext(args[0]));
code_txt = ____load(args[0]);
code=reflection.parse_code(code_txt);
for(i = 1;i<args.length;i++)
{
code.args.add(args[i]);
}
code.run();
}else{
console.writeln("tlang <app.tlang,code_files.txt>");
}
};

View File

@ -12,4 +12,9 @@
<EmbeddedResource Include="launcher\launcher.tlang" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>