Skip to content

Commit

Permalink
Merge pull request #32 from AIexandrKotov/sltbase
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
AIexandrKotov authored Nov 6, 2023
2 parents 2588653 + ac69fc1 commit 2a8c4ad
Show file tree
Hide file tree
Showing 55 changed files with 1,258 additions and 248 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ end.

### LANG and REPL compatibility

| LANG version | REPL version |
|--------------|--------------|
| 0.2.0 | 1.0.0+ |
| 0.1.0-0.1.1 | Unsupported |
| REPL version | LANG version |
|-----------------|-----------------|
| 1.0.0 | 0.2.0+ |

### Download
[![stable](https://img.shields.io/badge/REPL_stable-1.0.0-00cc00)](https://github.com/AIexandrKotov/SLThree/releases/tag/0.2.0) [![stable](https://img.shields.io/badge/LANG_exp-0.2.0-ccaa00)](https://github.com/AIexandrKotov/SLThree/releases/tag/0.2.0)
[![stable](https://img.shields.io/badge/REPL_stable-1.0.0-00cc00)](https://github.com/AIexandrKotov/SLThree/releases/tag/0.2.0) [![stable](https://img.shields.io/badge/LANG_exp-0.3.0-ccaa00)](https://github.com/AIexandrKotov/SLThree/releases/tag/0.3.0)
14 changes: 13 additions & 1 deletion SLThree/Embedding/EmbeddingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public static ExecutionContext RunScript(this ExecutionContext context, string c
{
return Parser.This.RunScript(code, null, context);
}
public static T Unwrap<T>(this ExecutionContext context) where T : new() => Wrapper<T>.Unwrap(context);

public static T Unwrap<T>(this ExecutionContext context) where T : new() => UnwrapperForInstances<T>.Unwrap(context);
public static void UnwrapStatic<T>(this ExecutionContext context) => Wrapper<T>.UnwrapStatic(context);
public static ExecutionContext Wrap<T>(this T obj) => Wrapper<T>.Wrap(obj);
public static ExecutionContext WrapStatic<T>() => Wrapper<T>.WrapStatic();
public static void UnwrapStaticClass(this ExecutionContext context, Type type) => UnwrapperForStaticClasses.Unwrap(type, context);
public static void UnwrapStaticClass(this Type type, ExecutionContext context) => UnwrapperForStaticClasses.Unwrap(type, context);
public static ExecutionContext WrapStaticClass(this Type type) => UnwrapperForStaticClasses.Wrap(type);

public static T SafeUnwrap<T>(this ExecutionContext context) where T : new() => UnwrapperForInstances<T>.SafeUnwrap(context);
public static void SafeUnwrapStatic<T>(this ExecutionContext context) => Wrapper<T>.SafeUnwrapStatic(context);
public static void SafeUnwrapStaticClass(this ExecutionContext context, Type type) => UnwrapperForStaticClasses.SafeUnwrap(type, context);
public static void SafeUnwrapStaticClass(this Type type, ExecutionContext context) => UnwrapperForStaticClasses.SafeUnwrap(type, context);
}
}
9 changes: 6 additions & 3 deletions SLThree/Exceptions/OperatorError.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using SLThree.Extensions;
using System;

namespace SLThree
{
public class OperatorError : RuntimeError
{
public OperatorError(ExpressionUnary unary, Type left)
: base($"Operator {unary.Operator} not allow for {left?.Name ?? "null"}", unary.SourceContext) { }
: base($"Operator {unary.Operator} not allow for {left?.GetTypeString() ?? "null"}", unary.SourceContext) { }
public OperatorError(ExpressionBinary binary, Type left, Type right)
: base($"Operator {binary.Operator} not allow for {left?.Name ?? "null"} and {right?.Name ?? "null"}", binary.SourceContext) { }
: base($"Operator {binary.Operator} not allow for {left?.GetTypeString() ?? "null"} and {right?.GetTypeString() ?? "null"}", binary.SourceContext) { }
public OperatorError(string op, Type cond, SourceContext context)
: base($"Operator {op} not allow for {cond?.GetTypeString() ?? "null"}", context) { }
}
}
3 changes: 3 additions & 0 deletions SLThree/Exceptions/SLTException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public SyntaxError() : base() { }
public SyntaxError(SourceContext context) : base(context) { }
public SyntaxError(string message, SourceContext context) : base(message, context) { }
public SyntaxError(string message, Exception inner, SourceContext context) : base(message, inner, context) { }
public SyntaxError(Cursor cursor) : base(new SourceContext(cursor)) { }
public SyntaxError(string message, Cursor cursor) : base(message, new SourceContext(cursor)) { }
public SyntaxError(string message, Exception inner, Cursor cursor) : base(message, inner, new SourceContext(cursor)) { }
}

/* /// <summary>
Expand Down
23 changes: 14 additions & 9 deletions SLThree/ExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -12,24 +14,24 @@ public interface IExecutable
object GetValue(ExecutionContext context);
}

public bool ForbidImplicit = true;
/// <summary>
/// Запрещает implicit в контексте
/// </summary>
public bool fimp = false;

public bool Returned;
public bool Broken;
public bool Continued;

public object ReturnedValue;

public static ContextWrap global = new ContextWrap(new ExecutionContext());
public List<Exception> Errors = new List<Exception>();

public static ContextWrap global = new ContextWrap(new ExecutionContext() { fimp = true });

private static bool and(bool a, bool b) => a && b;
private static bool or(bool a, bool b) => a || b;
private static bool xor(bool a, bool b) => a ^ b;
static ExecutionContext()
{
global.pred.LocalVariables.SetValue("and", Method.Create<bool, bool, bool>(and));
global.pred.LocalVariables.SetValue("or", Method.Create<bool, bool, bool>(or));
global.pred.LocalVariables.SetValue("xor", Method.Create<bool, bool, bool>(xor));

}

public class ContextWrap
Expand All @@ -42,10 +44,11 @@ public ContextWrap(ExecutionContext pred)
}
internal ExecutionContext PreviousContext;
public ContextWrap pred => new ContextWrap(PreviousContext);
public ContextWrap direct => new ContextWrap(this);
public ContextWrap wrap => new ContextWrap(this);

private int cycles = 0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void StartCycle()
{
cycles += 1;
Expand All @@ -54,6 +57,7 @@ public void StartCycle()

public bool InCycle() => cycles > 1;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EndCycle()
{
cycles -= 1;
Expand All @@ -64,6 +68,7 @@ public void EndCycle()
public void Return(object o) { Returned = true; ReturnedValue = o; }
public void Break() { Broken = true; }
public void Continue() { Continued = true; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PrepareToInvoke() { Returned = Broken = Continued = false; }
public void DefaultEnvironment()
{
Expand Down
9 changes: 9 additions & 0 deletions SLThree/Extensions/GenericExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -20,6 +22,7 @@ public static string JoinIntoString<T>(this IEnumerable<T> e, string delim)
return sb.ToString();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TOut[] ConvertAll<TIn, TOut>(this TIn[] array, Converter<TIn, TOut> func)
=> Array.ConvertAll(array, func);

Expand All @@ -39,6 +42,12 @@ public static string ReplaceAll(this string str, IDictionary<string, string> rep

public static T ToEnum<T>(this string s) where T : Enum => (T)Enum.Parse(typeof(T), s);

public static IEnumerable<object> Enumerate(this IEnumerable enumerable)
{
foreach (var x in enumerable)
yield return x;
}

public static TOut Cast<TIn, TOut>(this TIn o) where TOut: TIn => (TOut)o;
public static T Cast<T>(this object o) => (T)o;
}
Expand Down
14 changes: 14 additions & 0 deletions SLThree/Extensions/SLTHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static object CastToMax(this object o)

public static string GetTypeString(this Type t)
{
if (t.IsGenericType)
return $"{t.FullName.Substring(0, t.FullName.IndexOf('`'))}<{t.GetGenericArguments().ConvertAll(x => x.GetTypeString()).JoinIntoString(", ")}>";
if (t == type_object) return "object";
if (t == type_byte) return "u8";
if (t == type_sbyte) return "i8";
Expand All @@ -45,6 +47,18 @@ public static string GetTypeString(this Type t)

else return t.FullName;
}

public static bool IsList(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == type_list;
}
public static bool IsDictionary(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == type_dict;
}
private static Type type_list = typeof(List<>);
private static Type type_dict = typeof(Dictionary<,>);

public static string GetTypeString(this string t)
{
if (t == "System.Object") return "object";
Expand Down
1 change: 1 addition & 0 deletions SLThree/Lexems/BaseLexem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Pegasus.Common;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace SLThree
{
Expand Down
23 changes: 23 additions & 0 deletions SLThree/Lexems/CreatorArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Pegasus.Common;
using SLThree.Extensions;
using System.Linq;

namespace SLThree
{
public class CreatorArray : BaseLexem
{
public BaseLexem[] Lexems;

public CreatorArray(BaseLexem[] lexems, Cursor cursor) : base(cursor)
{
Lexems = lexems;
}

public override object GetValue(ExecutionContext context)
{
return Lexems.ConvertAll(x => x.GetValue(context)).ToList();
}

public override string ToString() => $"[{Lexems.JoinIntoString(", ")}]";
}
}
40 changes: 40 additions & 0 deletions SLThree/Lexems/CreatorDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Pegasus.Common;
using SLThree.Extensions;
using System.Collections.Generic;
using System.Linq;

namespace SLThree
{
public class CreatorDictionary : BaseLexem
{
public class Entry : BaseLexem
{
public BaseLexem Key;
public BaseLexem Value;
public Entry(BaseLexem key, BaseLexem value, Cursor cursor) : base(cursor)
{
Key = key;
Value = value;
}
public override object GetValue(ExecutionContext context)
{
return new KeyValuePair<object, object>(Key.GetValue(context), Value.GetValue(context));
}
public override string ToString() => $"{Key}: {Value}";
}

public Entry[] Entries;

public CreatorDictionary(Entry[] entries, Cursor cursor) : base(cursor)
{
Entries = entries;
}

public override object GetValue(ExecutionContext context)
{
return Entries.Select(x => (KeyValuePair<object, object>)x.GetValue(context)).ToDictionary(x => x.Key, x => x.Value);
}

public override string ToString() => $"{{{Entries.JoinIntoString(", ")}}}";
}
}
47 changes: 47 additions & 0 deletions SLThree/Lexems/CreatorTuple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Pegasus.Common;
using SLThree.Extensions;
using System;
using System.Linq;
using System.Runtime.CompilerServices;

namespace SLThree
{
public class CreatorTuple : BaseLexem
{
public BaseLexem[] Lexems;

public CreatorTuple(BaseLexem[] lexems, Cursor cursor) : base(cursor)
{
Lexems = lexems;
}

public override object GetValue(ExecutionContext context)
{
return Create(Lexems.ConvertAll(x => x.GetValue(context)));
}

public static ITuple Create(object[] objs, int index = 0)
{
switch (objs.Length - index)
{
case 0: throw new ArgumentException("Zero length array in objs");
case 1: return new Tuple<object>(objs[index]);
case 2: return new Tuple<object, object>(objs[index], objs[index + 1]);
case 3: return new Tuple<object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2]);
case 4: return new Tuple<object, object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2], objs[index + 3]);
case 5: return new Tuple<object, object, object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2], objs[index + 3], objs[index + 4]);
case 6: return new Tuple<object, object, object, object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2], objs[index + 3], objs[index + 4], objs[index + 5]);
case 7: return new Tuple<object, object, object, object, object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2], objs[index + 3], objs[index + 4], objs[index + 5], objs[index + 6]);
default: return new Tuple<object, object, object, object, object, object, object, object>(objs[index + 0], objs[index + 1], objs[index + 2], objs[index + 3], objs[index + 4], objs[index + 5], objs[index + 6], Create(objs, index + 7));
}
}
public static object[] ToArray(ITuple tuple)
{
var ret = new object[tuple.Length];
for (var i = 0; i < tuple.Length; i++)
ret[i] = tuple[i];
return ret;
}
public override string ToString() => $"({Lexems.JoinIntoString(", ")})";
}
}
9 changes: 5 additions & 4 deletions SLThree/Lexems/Expressions/ExpressionBinaryAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override object GetValue(ExecutionContext context)
{
object left;
object right;
if (context.ForbidImplicit)
if (context.fimp)
{
left = Left.GetValue(context);
right = Right.GetValue(context);
Expand All @@ -25,8 +25,8 @@ public override object GetValue(ExecutionContext context)
}
if (left is long i1)
{
if (right is double d2) return i1 + d2;
if (right is long i2) return i1 + i2;
if (right is double d2) return i1 + d2;
}
else if (left is double d1)
{
Expand All @@ -36,10 +36,11 @@ public override object GetValue(ExecutionContext context)
}
else if (left is ulong u1)
{
if (right is double d2) return u1 + d2;
if (right is ulong u2) return u1 + u2;
if (right is double d2) return u1 + d2;
}
throw new OperatorError(this, left?.GetType(), right?.GetType());
context.Errors.Add(new OperatorError(this, left?.GetType(), right?.GetType()));
return null;
}
}
}
19 changes: 19 additions & 0 deletions SLThree/Lexems/Expressions/ExpressionBinaryAnd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Pegasus.Common;

namespace SLThree
{
public class ExpressionBinaryAnd : ExpressionBinary
{
public override string Operator => "&&";
public ExpressionBinaryAnd(BaseLexem left, BaseLexem right, Cursor cursor) : base(left, right, cursor) { }
public ExpressionBinaryAnd() : base() { }
public override object GetValue(ExecutionContext context)
{
object left = Left.GetValue(context);
object right = Right.GetValue(context);
if (left is bool b1 && right is bool b2) return b1 && b2;
context.Errors.Add(new OperatorError(this, left?.GetType(), right?.GetType()));
return null;
}
}
}
18 changes: 12 additions & 6 deletions SLThree/Lexems/Expressions/ExpressionBinaryAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ public override object GetValue(ExecutionContext context)
}
else
{
if (Left is MemberAccess memberAccess)
if (Left is NameLexem nl)
{
variable_index = context.LocalVariables.SetValue(nl.Name, right);
is_namelexem = true;
counted_invoked = context;
return right;
}
else if (Left is MemberAccess memberAccess)
{
memberAccess.SetValue(context, right);
return right;
}
else if (Left is NameLexem nl)
else if (Left is IndexLexem indexLexem)
{
variable_index = context.LocalVariables.SetValue(nl.Name, right);
is_namelexem = true;
counted_invoked = context;
indexLexem.SetValue(context, right);
return right;
}
}
throw new OperatorError(this, Left?.GetType(), right?.GetType());
context.Errors.Add(new OperatorError(this, Left?.GetType(), right?.GetType()));
return null;
}
}
}
Loading

0 comments on commit 2a8c4ad

Please sign in to comment.