Rearrange files

This commit is contained in:
Mike Nolan 2023-03-09 22:33:58 -06:00
parent f28f949d0f
commit 1f5f0ce0d4
43 changed files with 543 additions and 165 deletions

View File

@ -1,128 +0,0 @@
namespace tlang;
class Program
{
static int Main(string[] args)
{
//Lexer lexer = new Lexer("testString.tlang");
Parser parser = new Parser();
//parser.Add(lexer.Tokens);
int r=0;
RootEnvironment env=new RootEnvironment();
parser.LoadEnvironment(env);
env["array"] = new TExternalMethod((args2)=>{
return new TArray();
});
env["dict"] = new TExternalMethod((args2)=>{
return new TDictionary();
});
env["print"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
return new TNumber(args2.Length);
});
env["println"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
Console.WriteLine();
return new TNumber(args2.Length);
});
env["int"] = new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var arg = args2[0];
var sArg = arg as TString;
var dArg = arg as TNumber;
if(sArg != null)
{
long val;
if(long.TryParse(sArg.Value,out val))
{
return new TNumber(val);
}
}
if(dArg != null)
{
return new TNumber(Math.Round(dArg.Value));
}
}
return TObject.Null;
});
env["filecreate"]=new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var fileName = args2[0] as TString;
if(fileName != null)
{
return new TInternalObject(File.Create(fileName.Value));
}
}
return new TNull();
});
env["streamwritestr"]=new TExternalMethod((args2)=>{
if(args2.Length == 2)
{
var internalObj = args2[0] as TInternalObject;
if(internalObj != null)
{
var strm = internalObj.Object as Stream;
if(strm != null)
{
var str = args2[1] as TString;
if(str != null)
{
var bytes=System.Text.Encoding.UTF8.GetBytes(str.Value);
strm.Write(bytes,0,bytes.Length);
}
}
}
}
return TObject.Null;
});
env["streamclose"]=new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var internalObj = args2[0] as TInternalObject;
if(internalObj != null)
{
var strm = internalObj.Object as Stream;
if(strm != null)
{
strm.Dispose();
}
}
}
return TObject.Null;
});
Lexer lexer =new Lexer(new StreamReader("file.tlang"));
var res=parser.ParseExpression(lexer.Tokens,ref r,true).Execute(env) as TNumber;
var main = env["main"] as ICallable;
if(main != null)
{
List<TString> args2=new List<TString>();
foreach(var argument in args)
{
args2.Add(new TString(argument));
}
res=main.Call(args2.ToArray()) as TNumber;
}
if(res != null)
{
return (int)res.Value;
}
return 0;
}
}

View File

@ -1,8 +0,0 @@
each(dir : fs.enumerate_dirs(fs.home))
{
println(dir);
}
each(file : fs.enumerate_files(fs.home))
{
println(file);
}

9
tlang/Program.cs Normal file
View File

@ -0,0 +1,9 @@
using tlang;
List<LexToken> tokens;
using(var sr = new StreamReader(ApplicationRunner.GetLauncherScript()))
{
var lexer=new Lexer(sr);
tokens=lexer.Tokens;
}
Environment.Exit(ApplicationRunner.Run(tokens,args));

1
tlang/code.tlang Normal file
View File

@ -0,0 +1 @@
console.writeln(42);

View File

@ -1,5 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\tlanglib\tlanglib.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>

View File

@ -1,6 +1,6 @@
namespace tlang
{
internal class LexToken
public class LexToken
{
private LexToken(string text)
{

View File

@ -2,7 +2,7 @@ using System.Text;
namespace tlang
{
internal class Lexer
public class Lexer
{
TextReader reader;
public Lexer(string fName) : this(new StreamReader(fName))
@ -13,17 +13,19 @@ namespace tlang
{
this.reader = reader;
}
List<LexToken>? tokens=null;
List<LexToken> tokens=new List<LexToken>();
public List<LexToken> Tokens
{
get{
if(tokens == null)
if(!set)
{
set=true;
tokens = _getTokens();
}
return tokens;
}
}
bool set=false;
List<LexToken> _tokens = new List<LexToken>();
StringBuilder b = new StringBuilder();
private void FlushBuilder()
@ -44,7 +46,8 @@ namespace tlang
read = reader.Read();
if(read == 'x')
{
return (char)Convert.FromHexString($"{(char)reader.Read()}{(char)reader.Read()}")[0];
return (char)short.Parse($"0x{(char)reader.Read()}{(char)reader.Read()}",System.Globalization.NumberStyles.AllowHexSpecifier);
}
else if(read == 'n')
{
@ -89,7 +92,7 @@ namespace tlang
break;
case '\'':
FlushBuilder();
LexToken.FromChar(getChar().ToString());
_tokens.Add(LexToken.FromChar(getChar().ToString()));
reader.Read();
break;
case '#':
@ -200,23 +203,10 @@ namespace tlang
case ':':
case ',':
case ';':
case '=':
FlushBuilder();
_tokens.Add(LexToken.FromGeneralToken(read));
break;
case '=':
FlushBuilder();
if(next == read || next == '>')
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
break;
case ' ':
case '\n':
case '\t':

View File

@ -13,6 +13,7 @@ namespace tlang
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var res=Parent.Execute(nodeEnv);
var dict = res as TDictionary;
@ -48,6 +49,7 @@ namespace tlang
}
else if(array != null)
{
if(Text == "add")
{
if(Args.Count == 1)

View File

@ -3,12 +3,24 @@ namespace tlang
internal class Parser
{
public Parser()
public Parser(List<LexToken> tokens)
{
int i=0;
Node = ParseExpression(tokens,ref i,true);
}
public Node Node {get;set;}
public Node ParseExpression(List<LexToken> tokens,ref int i,bool isFirst=false)
public TObject Execute(IScopeEnvironment env,params TObject[] args)
{
var res=Node.Execute(env);
var main = env["main"] as ICallable;
if(main != null)
{
res=main.Call(args);
}
return res;
}
private Node ParseExpression(List<LexToken> tokens,ref int i,bool isFirst=false)
{
if(i>= tokens.Count) return new ConstNode(new TNumber(0));
if(tokens[i].Text == "{" || isFirst)
@ -39,7 +51,7 @@ namespace tlang
if(i<tokens.Count && tokens[i].Text == "=")
{
i++;
GetVariableValue? gvar = myExpression as GetVariableValue;
var gvar = myExpression as GetVariableValue;
if(gvar != null)
{
myExpression= new SetVariableNode(gvar,ParseSum(tokens,ref i));
@ -164,7 +176,7 @@ namespace tlang
{
i++;
Node ls = ParseExpression(tokens,ref i);
GetVariableValue? name = new GetVariableValue("item");
var name = new GetVariableValue("item");
if(i<tokens.Count && tokens[i].Text == ":")
{
i++;
@ -418,10 +430,316 @@ namespace tlang
}
return myExpression;
}
private TObject CreateStream(Stream strm)
{
TDictionary dict = new TDictionary();
dict["setposition"] = new TExternalMethod((args2)=>{
if(args2.Length ==1)
{
var pos = args2[0] as TNumber;
if(pos != null)
{
strm.Position = (long)pos.Value;
}
}
return TObject.Null;
});
dict["getposition"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Position);
});
dict["getlength"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Length);
});
dict["getcount"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Length);
});
dict["canread"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanRead ? 1 : 0);
});
dict["canwrite"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanWrite ? 1 : 0);
});
dict["canseek"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanSeek ? 1 : 0);
});
dict["write"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var bufAr = args2[0] as TArray;
if(args2.Length == 3)
{
//buffer,offset,len
var _offset = args2[1] as TNumber;
var _len = args2[2] as TNumber;
if(bufAr != null && _offset != null && _len != null)
{
int count = (int)_len.Value;
int offset = (int)_offset.Value;
int totalRead = Math.Min(bufAr.Items.Count - offset,count);
byte[] buffer = new byte[totalRead];
for(int i = 0;i<totalRead;i++)
{
var item = bufAr.Items[i+offset];
var c = item as TChar;
var n = item as TNumber;
if(c != null) buffer[i] = (byte)c.Value;
if(n != null) buffer[i] = (byte)n.Value;
}
strm.Write(buffer,0,buffer.Length);
return new TNumber(totalRead);
}
}
else
{
var _len = args2[1] as TNumber;
if(bufAr != null && _len != null)
{
int count = (int)_len.Value;
int totalRead = Math.Min(bufAr.Items.Count,count);
byte[] buffer = new byte[totalRead];
for(int i = 0;i<totalRead;i++)
{
var item = bufAr.Items[i];
var c = item as TChar;
var n = item as TNumber;
if(c != null) buffer[i] = (byte)c.Value;
if(n != null) buffer[i] = (byte)n.Value;
}
strm.Write(buffer,0,buffer.Length);
return new TNumber(totalRead);
}
}
}return new TNumber(0);
});
dict["read"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var bufAr = args2[0] as TArray;
if(args2.Length == 3)
{
//buffer,offset,len
var _offset = args2[1] as TNumber;
var _len = args2[2] as TNumber;
if(bufAr != null && _offset != null && _len != null)
{
int count = (int)_len.Value;
int offset = (int)_offset.Value;
int totalRead = Math.Min(bufAr.Items.Count - offset,count);
byte[] buffer = new byte[totalRead];
totalRead=strm.Read(buffer,0,buffer.Length);
for(int i = 0;i<totalRead;i++)
{
bufAr.Items[i+offset] = new TNumber(buffer[i]);
}
return new TNumber(totalRead);
}
}
else
{
var _len = args2[1] as TNumber;
if(bufAr != null && _len != null)
{
int count = (int)_len.Value;
int totalRead = Math.Min(bufAr.Items.Count,count);
byte[] buffer = new byte[totalRead];
totalRead=strm.Read(buffer,0,buffer.Length);
for(int i = 0;i<totalRead;i++)
{
bufAr.Items[i] = new TNumber(buffer[i]);
}
return new TNumber(totalRead);
}
}
}return new TNumber(0);
});
dict["close"] = new TExternalMethod((args2)=>{
strm.Dispose();
return TObject.Uninit;
});
dict["flush"] = new TExternalMethod((args2)=>{
strm.Flush();
return TObject.Uninit;
});
return dict;
}
public void LoadEnvironment(RootEnvironment env)
{
TDictionary fs = new TDictionary();
fs["file_exists"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(File.Exists(fileName)) return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["dir_exists"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(Directory.Exists(fileName)) return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["mkdir"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(Directory.Exists(fileName)) return new TNumber(2);
Directory.CreateDirectory(fileName);
return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["create"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.Create(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["openread"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.OpenRead(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["openwrite"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.OpenWrite(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["append"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.Open(fileName,FileMode.Append));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["readalltext"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(!File.Exists(fileName)) return TObject.Null;
return new TString(File.ReadAllText(fileName));
}catch(Exception ex)
{
_=ex;
}
}
}
return TObject.Null;
});
fs["writealltext"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var fileName=args2[0].ToString();
var text = args2[1].ToString();
if(!string.IsNullOrWhiteSpace(fileName) && text != null)
{
try{
File.WriteAllText(fileName,text);
return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["home"] = new TString(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
fs["enumerate_files"] = new TExternalMethod((args)=>{
if(args.Length > 0)
@ -477,6 +795,105 @@ namespace tlang
return TObject.Null;
});
env["fs"]=fs;
TDictionary reflection = new TDictionary();
reflection["parse_code"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var str = args2[0] as TString;
if(str != null)
{
Lexer lexer = new Lexer(new StringReader(str.Value));
try{
Parser _parser=new Parser(lexer.Tokens);
RootEnvironment env2 = new RootEnvironment();
TDictionary rootDict = new TRootDict(env2);
LoadEnvironment(env2);
TDictionary parseCodeVal = new TDictionary();
var argsArray= new TArray();
parseCodeVal["root"] = rootDict;
parseCodeVal["args"] = argsArray;
parseCodeVal["run"] = new TExternalMethod((args4)=>{
//run()
try{
return _parser.Execute(env2,argsArray.Items.ToArray());
}catch(Exception ex)
{
_=ex;
return TObject.Null;
}
});
return parseCodeVal;
}catch(Exception ex)
{
_=ex;
return TObject.Null;
}
}
}
return TObject.Uninit;
});
env["reflection"] = reflection;
var con = new TDictionary();
con["write"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
return new TNumber(args2.Length);
});
con["writeln"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
Console.WriteLine();
return new TNumber(args2.Length);
});
var instance = new TDictionary();
instance["array"] = new TExternalMethod((args2)=>{
return new TArray();
});
instance["dict"] = new TExternalMethod((args2)=>{
return new TDictionary();
});
env["create"] =instance;
env["console"]=con;
env["int"] = new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var arg = args2[0];
var sArg = arg as TString;
var dArg = arg as TNumber;
if(sArg != null)
{
long val;
if(long.TryParse(sArg.Value,out val))
{
return new TNumber(val);
}
}
if(dArg != null)
{
return new TNumber(Math.Round(dArg.Value));
}
}
return TObject.Null;
});
}

48
tlanglib/Program.cs Normal file
View File

@ -0,0 +1,48 @@
namespace tlang
{
public class ApplicationRunner
{
public static Stream GetLauncherScript()
{
var fileName = System.Environment.GetEnvironmentVariable("TLANG_LAUNCHER_SCRIPT");
if(!string.IsNullOrWhiteSpace(fileName))
{
return File.OpenRead(fileName);
}
return typeof(ApplicationRunner).Assembly.GetManifestResourceStream("tlanglib.launcher.launcher.tlang");
}
public static int Run(List<LexToken> code,string[] args)
{
RootEnvironment env=new RootEnvironment();
Parser parser=new Parser(code);
parser.LoadEnvironment(env);
List<TString> args2=new List<TString>();
foreach(var argument in args)
{
args2.Add(new TString(argument));
}
var res= parser.Execute(env,args2.ToArray()) as TNumber;
if(res != null)
{
return (int)res.Value;
}
return 0;
}
}
}

View File

@ -1,5 +1,25 @@
namespace tlang
{
public class TRootDict : TDictionary
{
IScopeEnvironment env;
public TRootDict(IScopeEnvironment env)
{
this.env=env;
}
public override TObject GetMember(string name)
{
return env.GetVariable(name);
}
public override void SetMember(string name, TObject obj)
{
env.SetVariable(name,obj);
}
public override bool MemberExists(string name)
{
return env.VariableExists(name);
}
}
public class TDictionary : TObject
{
public override bool AsBoolean {get =>true;}
@ -10,7 +30,7 @@ namespace tlang
Dictionary<string,TObject> items {get;set;}=new Dictionary<string, TObject>();
public TObject GetMember(string name)
public virtual TObject GetMember(string name)
{
if(items.ContainsKey(name))
{
@ -20,7 +40,7 @@ namespace tlang
}
}
public void SetMember(string name, TObject obj)
public virtual void SetMember(string name, TObject obj)
{
if(items.ContainsKey(name))
{
@ -32,7 +52,7 @@ namespace tlang
}
}
public bool MemberExists(string name)
public virtual bool MemberExists(string name)
{
return items.ContainsKey(name);
}

View File

@ -53,7 +53,7 @@ namespace tlang
{
for(int i =0;i<Body.Arguments.Count;i++)
{
if(Body.Arguments[i].StartsWith('$'))
if(Body.Arguments[i].StartsWith("$"))
{
return i;
}

View File

@ -0,0 +1,7 @@
main = func($$args){
if(fs.file_exists(args[0]))
{
code = reflection.parse_code(fs.readalltext(args[0]));
code.run();
}
};

15
tlanglib/tlanglib.csproj Normal file
View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<OutputType>Exe</OutputType>-->
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="launcher\launcher.tlang" />
</ItemGroup>
</Project>