-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Использование последних наворотов шарпа (#26)
- Loading branch information
Showing
150 changed files
with
4,398 additions
and
4,697 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,53 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Interpreter.Lib.BackEnd.Values; | ||
using SystemType = System.Type; | ||
|
||
namespace Interpreter.Lib.BackEnd.Instructions | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
public class AsString : Simple | ||
{ | ||
public class AsString : Simple | ||
public AsString(string left, IValue right, int number) : | ||
base(left, (null, right), "", number) | ||
{ | ||
public AsString(string left, IValue right, int number) : | ||
base(left, (null, right), "", number) | ||
{ | ||
} | ||
} | ||
|
||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[Left] = JsonSerializer.Serialize( | ||
right.right.Get(frame), | ||
new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
ReferenceHandler = ReferenceHandler.IgnoreCycles, | ||
Converters = { new DoubleValueWriteConverter() }, | ||
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | ||
} | ||
); | ||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[Left] = JsonSerializer.Serialize( | ||
right.right.Get(frame), | ||
new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
ReferenceHandler = ReferenceHandler.IgnoreCycles, | ||
Converters = { new DoubleValueWriteConverter() }, | ||
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | ||
} | ||
); | ||
|
||
return Jump(); | ||
} | ||
return Jump(); | ||
} | ||
|
||
protected override string ToStringRepresentation() => $"{Left} = {right.right} as string"; | ||
protected override string ToStringRepresentation() => $"{Left} = {right.right} as string"; | ||
|
||
[ExcludeFromCodeCoverage] | ||
private class DoubleValueWriteConverter : JsonConverter<double> | ||
{ | ||
public override double Read(ref Utf8JsonReader reader, | ||
Type typeToConvert, JsonSerializerOptions options) => | ||
throw new NotImplementedException(); | ||
[ExcludeFromCodeCoverage] | ||
private class DoubleValueWriteConverter : JsonConverter<double> | ||
{ | ||
public override double Read(ref Utf8JsonReader reader, | ||
SystemType typeToConvert, JsonSerializerOptions options) => | ||
throw new NotImplementedException(); | ||
|
||
public override void Write(Utf8JsonWriter writer, | ||
double value, JsonSerializerOptions options) | ||
{ | ||
// ReSharper disable once CompareOfFloatsByEqualityOperator | ||
if (value == Math.Truncate(value)) | ||
writer.WriteNumberValue(Convert.ToInt64(value)); | ||
else | ||
writer.WriteNumberValue(value); | ||
} | ||
public override void Write(Utf8JsonWriter writer, | ||
double value, JsonSerializerOptions options) | ||
{ | ||
// ReSharper disable once CompareOfFloatsByEqualityOperator | ||
if (value == Math.Truncate(value)) | ||
writer.WriteNumberValue(Convert.ToInt64(value)); | ||
else | ||
writer.WriteNumberValue(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
namespace Interpreter.Lib.BackEnd.Instructions | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
public class BeginFunction : Instruction | ||
{ | ||
public class BeginFunction : Instruction | ||
{ | ||
private readonly FunctionInfo _function; | ||
private readonly FunctionInfo _function; | ||
|
||
public BeginFunction(int number, FunctionInfo function) : base(number) | ||
{ | ||
_function = function; | ||
} | ||
public BeginFunction(int number, FunctionInfo function) : base(number) | ||
{ | ||
_function = function; | ||
} | ||
|
||
public override int Execute(VirtualMachine vm) => Number + 1; | ||
public override int Execute(VirtualMachine vm) => Number + 1; | ||
|
||
protected override string ToStringRepresentation() => $"BeginFunction {_function.CallId()}"; | ||
} | ||
protected override string ToStringRepresentation() => $"BeginFunction {_function.CallId()}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
namespace Interpreter.Lib.BackEnd.Instructions | ||
public class CallFunction : Simple | ||
{ | ||
public class CallFunction : Simple | ||
{ | ||
private readonly FunctionInfo _function; | ||
private readonly int _numberOfArguments; | ||
private readonly FunctionInfo _function; | ||
private readonly int _numberOfArguments; | ||
|
||
public CallFunction(FunctionInfo function, int number, int numberOfArguments, string left = null) : | ||
base(left, (null, null), "Call ", number) | ||
{ | ||
_function = function; | ||
_numberOfArguments = numberOfArguments + Convert.ToInt32(function.MethodOf != null); | ||
} | ||
public CallFunction(FunctionInfo function, int number, int numberOfArguments, string left = null) : | ||
base(left, (null, null), "Call ", number) | ||
{ | ||
_function = function; | ||
_numberOfArguments = numberOfArguments + Convert.ToInt32(function.MethodOf != null); | ||
} | ||
|
||
public override int Jump() => _function.Location; | ||
public override int Jump() => _function.Location; | ||
|
||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = new Frame(Number + 1, vm.Frames.Peek()); | ||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = new Frame(Number + 1, vm.Frames.Peek()); | ||
|
||
var i = 0; | ||
var args = new List<(string Id, object Value)>(); | ||
while (i < _numberOfArguments) | ||
{ | ||
args.Add(vm.Arguments.Pop()); | ||
frame[args[i].Id] = args[i].Value; | ||
i++; | ||
} | ||
var i = 0; | ||
var args = new List<(string Id, object Value)>(); | ||
while (i < _numberOfArguments) | ||
{ | ||
args.Add(vm.Arguments.Pop()); | ||
frame[args[i].Id] = args[i].Value; | ||
i++; | ||
} | ||
|
||
if (_function.MethodOf != null) | ||
if (_function.MethodOf != null) | ||
{ | ||
var obj = (Dictionary<string, object>) frame[_function.MethodOf]; | ||
foreach (var (key, value) in obj) | ||
{ | ||
var obj = (Dictionary<string, object>) frame[_function.MethodOf]; | ||
foreach (var (key, value) in obj) | ||
{ | ||
frame[key] = value; | ||
} | ||
frame[key] = value; | ||
} | ||
|
||
vm.CallStack.Push(new Call(Number, _function, args, Left)); | ||
vm.Frames.Push(frame); | ||
return _function.Location; | ||
} | ||
|
||
protected override string ToStringRepresentation() => Left == null | ||
? $"Call {_function}, {_numberOfArguments}" | ||
: $"{Left} = Call {_function}, {_numberOfArguments}"; | ||
vm.CallStack.Push(new Call(Number, _function, args, Left)); | ||
vm.Frames.Push(frame); | ||
return _function.Location; | ||
} | ||
|
||
protected override string ToStringRepresentation() => Left == null | ||
? $"Call {_function}, {_numberOfArguments}" | ||
: $"{Left} = Call {_function}, {_numberOfArguments}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
using System.Linq; | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
namespace Interpreter.Lib.BackEnd.Instructions | ||
public class CreateArray : Instruction | ||
{ | ||
public class CreateArray : Instruction | ||
{ | ||
private readonly string _id; | ||
private readonly int _size; | ||
private readonly string _id; | ||
private readonly int _size; | ||
|
||
public CreateArray(int number, string id, int size) : base(number) | ||
{ | ||
_id = id; | ||
_size = size; | ||
} | ||
|
||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[_id] = new object[_size].ToList(); | ||
return Number + 1; | ||
} | ||
public CreateArray(int number, string id, int size) : base(number) | ||
{ | ||
_id = id; | ||
_size = size; | ||
} | ||
|
||
protected override string ToStringRepresentation() => $"array {_id} = [{_size}]"; | ||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[_id] = new object[_size].ToList(); | ||
return Number + 1; | ||
} | ||
|
||
protected override string ToStringRepresentation() => $"array {_id} = [{_size}]"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
using System.Collections.Generic; | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
namespace Interpreter.Lib.BackEnd.Instructions | ||
public class CreateObject : Instruction | ||
{ | ||
public class CreateObject : Instruction | ||
{ | ||
private readonly string _id; | ||
private readonly string _id; | ||
|
||
public CreateObject(int number, string id) : base(number) | ||
{ | ||
_id = id; | ||
} | ||
|
||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[_id] = new Dictionary<string, object>(); | ||
return Number + 1; | ||
} | ||
public CreateObject(int number, string id) : base(number) | ||
{ | ||
_id = id; | ||
} | ||
|
||
protected override string ToStringRepresentation() => $"object {_id} = {{}}"; | ||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
frame[_id] = new Dictionary<string, object>(); | ||
return Number + 1; | ||
} | ||
|
||
protected override string ToStringRepresentation() => $"object {_id} = {{}}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
using System.Collections.Generic; | ||
using Interpreter.Lib.BackEnd.Values; | ||
|
||
namespace Interpreter.Lib.BackEnd.Instructions | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
public class DotAssignment : Simple | ||
{ | ||
public class DotAssignment : Simple | ||
public DotAssignment(string left, (IValue left, IValue right) right, int number) : | ||
base(left, right, ".", number) | ||
{ | ||
public DotAssignment(string left, (IValue left, IValue right) right, int number) : | ||
base(left, right, ".", number) | ||
{ | ||
} | ||
|
||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
var obj = (Dictionary<string, object>) frame[Left]; | ||
var field = (string) right.left.Get(frame) ?? string.Empty; | ||
obj[field] = right.right.Get(frame); | ||
return Number + 1; | ||
} | ||
} | ||
|
||
protected override string ToStringRepresentation() => | ||
$"{Left}{@operator}{right.left} = {right.right}"; | ||
public override int Execute(VirtualMachine vm) | ||
{ | ||
var frame = vm.Frames.Peek(); | ||
var obj = (Dictionary<string, object>) frame[Left]; | ||
var field = (string) right.left.Get(frame) ?? string.Empty; | ||
obj[field] = right.right.Get(frame); | ||
return Number + 1; | ||
} | ||
|
||
protected override string ToStringRepresentation() => | ||
$"{Left}{@operator}{right.left} = {right.right}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
namespace Interpreter.Lib.BackEnd.Instructions | ||
namespace Interpreter.Lib.BackEnd.Instructions; | ||
|
||
public class Goto : Instruction | ||
{ | ||
public class Goto : Instruction | ||
{ | ||
protected int jump; | ||
protected int jump; | ||
|
||
public Goto(int jump, int number) : base(number) | ||
{ | ||
this.jump = jump; | ||
} | ||
public Goto(int jump, int number) : base(number) | ||
{ | ||
this.jump = jump; | ||
} | ||
|
||
public override int Jump() => jump; | ||
public override int Jump() => jump; | ||
|
||
public override int Execute(VirtualMachine vm) => Jump(); | ||
public override int Execute(VirtualMachine vm) => Jump(); | ||
|
||
public void SetJump(int newJump) => jump = newJump; | ||
public void SetJump(int newJump) => jump = newJump; | ||
|
||
protected override string ToStringRepresentation() => $"Goto {Jump()}"; | ||
} | ||
protected override string ToStringRepresentation() => $"Goto {Jump()}"; | ||
} |
Oops, something went wrong.