diff --git a/lib/vein.ast.v2/syntax/ITransform.cs b/lib/vein.ast.v2/syntax/ITransform.cs index 9934ee55..0c1cc82c 100644 --- a/lib/vein.ast.v2/syntax/ITransform.cs +++ b/lib/vein.ast.v2/syntax/ITransform.cs @@ -1,4 +1,4 @@ -namespace vein.ast.v2.syntax; +namespace vein.ast; using Superpower.Model; diff --git a/lib/vein.ast.v2/syntax/TextParserExtensions.cs b/lib/vein.ast.v2/syntax/TextParserExtensions.cs index 90ff1f4b..5293c865 100644 --- a/lib/vein.ast.v2/syntax/TextParserExtensions.cs +++ b/lib/vein.ast.v2/syntax/TextParserExtensions.cs @@ -1,4 +1,4 @@ -namespace vein.ast.v2.syntax; +namespace vein.ast; using Superpower; using Superpower.Model; diff --git a/lib/vein.ast.v2/syntax/VeinOperators.cs b/lib/vein.ast.v2/syntax/VeinOperators.cs index 4c077530..57747ebd 100644 --- a/lib/vein.ast.v2/syntax/VeinOperators.cs +++ b/lib/vein.ast.v2/syntax/VeinOperators.cs @@ -1,181 +1,42 @@ -namespace vein.ast.v2.syntax; +namespace vein.ast; using Newtonsoft.Json.Converters; using Superpower; +using Superpower.Display; using Superpower.Model; using Superpower.Parsers; using System; - -public class VeinOperators -{ - -} - -public abstract record BaseSyntax : ITransform -{ - public abstract SyntaxType Kind { get; } - - public TextSpan Transform { get; set; } - public TextSpan TransformUntil { get; set; } -} - -public abstract record ExpressionSyntax : BaseSyntax -{ - public string ExpressionValue { get; protected set; } -} - -public record IdentifierExpression : ExpressionSyntax -{ - public IdentifierExpression(string exp) => ExpressionValue = exp; - - public override SyntaxType Kind => SyntaxType.IDENTIFIER; -} - -public enum SyntaxType -{ - IDENTIFIER -} - -public class VeinParseBase -{ - public TextParser RawIdentifier => - from first in Character.Letter.Or(Character.EqualTo('_').Or(Character.EqualTo('@'))) - from rest in Character.LetterOrDigit.Or(Character.EqualTo('_').Or(Character.EqualTo('@'))).Many() - select first + new string(rest); - - public TextParser IdentifierExpression => - RawIdentifier.Named("Identifier") - .Select(x => new IdentifierExpression(x)) - .Message("required valid identifier expression") - .WithLocation(); - -} - -public record VeinKeyword(string name); - - -public abstract class NumericLiteralExpressionSyntax(string value) -{ - public string Value { get; } = value; -} - -public class SByteValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public class ByteValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public class ShortValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public class UShortValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public class Int32ValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public class UInt32ValueExpressionSyntax(string value) : NumericLiteralExpressionSyntax(value); - -public abstract class ExpressionSyntax2 -{ - public abstract int Evaluate(); +using System.Collections.Generic; +using System.Linq; + +public enum TokenType +{ + Number_Int8, + Number_Int16, + Number_Int32, + Number_Int64, + Number_Float32, + [Token(Category = "operator", Example = "+")] + Plus, + [Token(Category = "operator", Example = "-")] + Minus, + [Token(Category = "operator", Example = "*")] + Multiply, + [Token(Category = "operator", Example = "/")] + Divide, + [Token(Category = "operator", Example = "%")] + Modulo, + [Token(Category = "operator", Example = "^")] + Power, + [Token(Category = "operator", Example = "as")] + As, + [Token(Category = "operator", Example = "<")] + LessThan, + [Token(Category = "operator", Example = ">")] + GreaterThan, + OpenParen, + CloseParen, + Identifier, + Comma } -public class LiteralExpressionSyntax : ExpressionSyntax2 -{ - public NumericLiteralExpressionSyntax NumericLiteral { get; } - - public LiteralExpressionSyntax(NumericLiteralExpressionSyntax numericLiteral) - { - NumericLiteral = numericLiteral; - } - - public override int Evaluate() - { - return int.Parse(NumericLiteral.Value); - } -} - -public class BinaryExpressionSyntax(ExpressionSyntax2 left, char @operator, ExpressionSyntax2 right) - : ExpressionSyntax2 -{ - public ExpressionSyntax2 Left { get; } = left; - public ExpressionSyntax2 Right { get; } = right; - public char Operator { get; } = @operator; - - public override int Evaluate() - { - var leftValue = Left.Evaluate(); - var rightValue = Right.Evaluate(); - - return Operator switch - { - '+' => leftValue + rightValue, - '-' => leftValue - rightValue, - '*' => leftValue * rightValue, - '/' => leftValue / rightValue, - _ => throw new InvalidOperationException("Unknown operator") - }; - } -} - - -public static class NumericParsers -{ - public static readonly TextParser NumberParser = - from digits in Character.Digit.AtLeastOnce() - select new string(digits); - - public static readonly TextParser SuffixParser = - from suffix in Character.EqualToIgnoreCase('u').OptionalOrDefault() - select suffix.ToString(); - - public static readonly TextParser NumericLiteralExpression = - from number in NumberParser - from suffix in SuffixParser - select DetermineType(number, suffix); - - public static NumericLiteralExpressionSyntax DetermineType(string number, string suffix) - { - if (suffix == "u") - { - uint parsedValue = uint.Parse(number); - if (parsedValue <= byte.MaxValue) - return new ByteValueExpressionSyntax(number); - else if (parsedValue <= ushort.MaxValue) - return new UShortValueExpressionSyntax(number); - else if (parsedValue <= uint.MaxValue) - return new UInt32ValueExpressionSyntax(number); - } - else - { - int parsedValue = int.Parse(number); - if (parsedValue is >= sbyte.MinValue and <= sbyte.MaxValue) - return new SByteValueExpressionSyntax(number); - else if (parsedValue is >= short.MinValue and <= short.MaxValue) - return new ShortValueExpressionSyntax(number); - else if (parsedValue is >= int.MinValue and <= int.MaxValue) - return new Int32ValueExpressionSyntax(number); - } - - throw new InvalidOperationException("Unsupported numeric range or suffix."); - } - - public static readonly TextParser Literal = - NumericLiteralExpression.Select(numericLiteral => new LiteralExpressionSyntax(numericLiteral) as ExpressionSyntax2); - - public static readonly TextParser Parenthesized = - from lparen in Character.EqualTo('(') - from expr in Parse.Ref(() => Expression) - from rparen in Character.EqualTo(')') - select expr; - - public static readonly TextParser Factor = - Parenthesized.Or(Literal); - - public static readonly TextParser Term = - Parse.Chain(Character.In('*', '/'), - Factor, - (op, left, right) => new BinaryExpressionSyntax(left, op, right)); - - public static readonly TextParser Expression = - Parse.Chain(Character.In('+', '-'), - Term, - (op, left, right) => new BinaryExpressionSyntax(left, op, right)); - -} diff --git a/lib/vein.ast.v2/vein.ast.v2.csproj b/lib/vein.ast.v2/vein.ast.v2.csproj index e2f10a38..e321b5a4 100644 --- a/lib/vein.ast.v2/vein.ast.v2.csproj +++ b/lib/vein.ast.v2/vein.ast.v2.csproj @@ -15,7 +15,7 @@ - + diff --git a/lib/vein.std/std.socket.test/std.socket.test.vproj b/lib/vein.std/std.socket.test/std.socket.test.vproj index e197ce3e..57420331 100644 --- a/lib/vein.std/std.socket.test/std.socket.test.vproj +++ b/lib/vein.std/std.socket.test/std.socket.test.vproj @@ -4,4 +4,5 @@ author: github: 0xF6 license: MIT license packages: -- ..\std\std.vproj +- .\std.socket.test.vproj +- std@0.30.9.3 diff --git a/lib/vein.std/std/Out.vein b/lib/vein.std/std/Out.vein index c643ed27..821a63e9 100644 --- a/lib/vein.std/std/Out.vein +++ b/lib/vein.std/std/Out.vein @@ -14,6 +14,10 @@ public static class Out public static class Terminal { [native("__internal__", "print_any")] private extern static print_any(value: Object): Void; + + [native("__internal__", "@_readline")] + public extern static readline(): string; + public static pipe: TerminalPipe; public static white: string = "w"; diff --git a/lib/vein.std/std/icon.png b/lib/vein.std/std/icon.png index bbe6279a..62d97d13 100644 Binary files a/lib/vein.std/std/icon.png and b/lib/vein.std/std/icon.png differ diff --git a/lib/vein.std/std/test/numeric.test.vein b/lib/vein.std/std/test/numeric.test.vein index 7805d1a4..d6c113aa 100644 --- a/lib/vein.std/std/test/numeric.test.vein +++ b/lib/vein.std/std/test/numeric.test.vein @@ -63,4 +63,36 @@ private static class NumericTest Assert.equal(12 * two, 24); Assert.equal(15 * two, 30); } + + + + [test] + public static i16_mod(): void + { + let two = as(2); + Assert.equal(as(2) % two, as(0)); + Assert.equal(as(4) % two, as(0)); + Assert.equal(as(6) % two, as(0)); + Assert.equal(as(8) % two, as(0)); + Assert.equal(as(12) % two, as(0)); + Assert.equal(as(15) % two, as(1)); + + if (as(0) == as(12) % as(2)) { + Out.print("suka 12 % 2 == 0"); + } else { + Out.print("blyat 12 % 2 != 0"); + } + } + + [test] + public static i16_mul(): void + { + let two = as(2); + Assert.equal(as(2) * two, as(4)); + Assert.equal(as(4) * two, as(8)); + Assert.equal(as(6) * two, as(12)); + Assert.equal(as(8) * two, as(16)); + Assert.equal(as(12) * two, as(24)); + Assert.equal(as(15) * two, as(30)); + } } \ No newline at end of file diff --git a/test/astv2.test/UnitTest1.cs b/test/astv2.test/UnitTest1.cs new file mode 100644 index 00000000..fcc71130 --- /dev/null +++ b/test/astv2.test/UnitTest1.cs @@ -0,0 +1,93 @@ +namespace astv2.test; + +using Superpower; +using Superpower.Model; +using Superpower.Parsers; +using Superpower.Tokenizers; +using System.Globalization; +using System.Linq.Expressions; +using vein.ast; + +public class Tests +{ + + [Test] + public void Test1() + { + var tokenizer = new TokenizerBuilder() + .Ignore(Span.WhiteSpace) + .Match(Character.EqualTo('+'), TokenType.Plus) + .Match(Character.EqualTo('-'), TokenType.Minus) + .Match(Character.EqualTo('*'), TokenType.Multiply) + .Match(Character.EqualTo('/'), TokenType.Divide) + .Match(Character.EqualTo('('), TokenType.OpenParen) + .Match(Character.EqualTo(')'), TokenType.CloseParen) + .Match(DecimalFloat, TokenType.Number_Float32) + .Match(Numerics.IntegerInt32, TokenType.Number_Int32) + .Match(Numerics.IntegerInt64, TokenType.Number_Int64) + .Build(); + + + + var result = ArithmeticExpressionParser.Ast.Parse(tokenizer.Tokenize("182 + 1 * 95 / 9102")); + + } + + public static TextParser DecimalFloat { get; } = Numerics.Decimal.Select(span => float.Parse(span.ToStringValue(), CultureInfo.InvariantCulture)); + +} + +class ArithmeticExpressionParser +{ + static readonly TokenListParser Add = + Token.EqualTo(TokenType.Plus).Value(ExpressionType.AddChecked); + + static readonly TokenListParser Subtract = + Token.EqualTo(TokenType.Minus).Value(ExpressionType.SubtractChecked); + + static readonly TokenListParser Multiply = + Token.EqualTo(TokenType.Multiply).Value(ExpressionType.MultiplyChecked); + + static readonly TokenListParser Divide = + Token.EqualTo(TokenType.Divide).Value(ExpressionType.Divide); + + static readonly TokenListParser Modulo = + Token.EqualTo(TokenType.Modulo).Value(ExpressionType.Modulo); + + static readonly TokenListParser Power = + Token.EqualTo(TokenType.Power).Value(ExpressionType.Power); + + static readonly TokenListParser Constant = + Token.EqualTo(TokenType.Number_Int32) + .Apply(Numerics.IntegerInt32) + .Select(n => (Expression)Expression.Constant(n)) + .Or(Token.EqualTo(TokenType.Number_Float32) + .Apply(Numerics.Decimal) + .Select(f => (Expression)Expression.Constant(f))); + + + static readonly TokenListParser Factor = + (from lparen in Token.EqualTo(TokenType.OpenParen) + from expr in Parse.Ref(() => Expr) + from rparen in Token.EqualTo(TokenType.CloseParen) + select expr) + .Or(Constant); + + static readonly TokenListParser Operand = + (from sign in Token.EqualTo(TokenType.Minus) + from factor in Factor + select (Expression)Expression.Negate(factor)) + .Or(Factor).Named("expression"); + + static readonly TokenListParser Term = + Parse.Chain(Multiply.Or(Divide).Or(Modulo).Or(Power), Operand, Expression.MakeBinary); + + static readonly TokenListParser Expr = + Parse.Chain(Add.Or(Subtract), Term, Expression.MakeBinary); + + public static TokenListParser>> Lambda() where T : struct + => Expr.AtEnd().Select(body => Expression.Lambda>(body)); + + public static readonly TokenListParser Ast + = Expr.AtEnd(); +} diff --git a/test/astv2.test/astv2.test.csproj b/test/astv2.test/astv2.test.csproj new file mode 100644 index 00000000..4210c770 --- /dev/null +++ b/test/astv2.test/astv2.test.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/test/ishtar_collections_test/ishtar_collections_test.csproj b/test/ishtar_collections_test/ishtar_collections_test.csproj index 97028cd6..73de8b93 100644 --- a/test/ishtar_collections_test/ishtar_collections_test.csproj +++ b/test/ishtar_collections_test/ishtar_collections_test.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + true false true