Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Minsk.Tests/CodeAnalysis/EvaluationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Minsk.CodeAnalysis;
using Minsk.CodeAnalysis.Syntax;
using Xunit;

namespace Minsk.Tests.CodeAnalysis
{
public class EvaluationTests
{
[Theory]
[InlineData("1", 1)]
[InlineData("+1", 1)]
[InlineData("-1", -1)]
[InlineData("14 + 12", 26)]
[InlineData("12 - 3", 9)]
[InlineData("4 * 2", 8)]
[InlineData("9 / 3", 3)]
[InlineData("(10)", 10)]
[InlineData("12 == 3", false)]
[InlineData("3 == 3", true)]
[InlineData("12 != 3", true)]
[InlineData("3 != 3", false)]
[InlineData("false == false", true)]
[InlineData("true == false", false)]
[InlineData("false != false", false)]
[InlineData("true != false", true)]
[InlineData("true", true)]
[InlineData("false", false)]
[InlineData("!true", false)]
[InlineData("!false", true)]
[InlineData("(a = 10) * a", 100)]
public void SyntaxFact_GetText_RoundTrips(string text, object expectedValue)
{
var syntaxTree = SyntaxTree.Parse(text);
var compilation = new Compilation(syntaxTree);
var variables = new Dictionary<VariableSymbol, object>();
var result = compilation.evaluate(variables);

Assert.Empty(result.Diagnostics);
Assert.Equal(expectedValue, result.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

namespace Minsk.Tests.CodeAnalysis.Syntax
{
public class SyntaxFctTest
public class SyntaxFactTests
{
[Theory]
[MemberData(nameof(GetSyntaxKindData))]
public void SyntaxFact_GetText_RoundTrips(SyntaxKind kind)
{
var text = SyntaxFct.GetText(kind);
var text = SyntaxFacts.GetText(kind);
if (text == null)
return;

var tokens = SyntaxTree.ParseTokens(text);
var token = Assert.Single(tokens);
Assert.Equal(kind, token.Kind);
Assert.Equal(text, token.Text);
}

public static IEnumerable<object[]> GetSyntaxKindData()
Expand Down
23 changes: 21 additions & 2 deletions Minsk.Tests/Minsk.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- <Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
Expand All @@ -16,4 +16,23 @@
<ProjectReference Include="..\Minsk\Minsk.csproj" />
</ItemGroup>

</Project>
</Project> -->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Minsk\Minsk.csproj" />
</ItemGroup>

</Project>
58 changes: 41 additions & 17 deletions Minsk/CodeAnalysis/Binding/Binder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Minsk.CodeAnalysis.Syntax;

namespace Minsk.CodeAnalysis.Binding
Expand All @@ -9,9 +10,9 @@ internal sealed class Binder
{
private readonly DiagnosticBag _diagnostics = new DiagnosticBag();

private readonly Dictionary<string, object> _variables;
private readonly Dictionary<VariableSymbol, object> _variables;

public Binder(Dictionary<string, object> variables)
public Binder(Dictionary<VariableSymbol, object> variables)
{
_variables = variables;
}
Expand Down Expand Up @@ -46,33 +47,56 @@ private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSynta

private BoundExpression BindAssignmentExpression(AssignmentExpressionSyntax syntax)
{
// var name = syntax.IdentifierToken.Text;
// var boundExpression = BindExpression(syntax.Expression);
// var defaultValue =
// boundExpression.Type == typeof(int)
// ? (object)0
// : boundExpression.Type == typeof(bool)
// ? (object)false
// : null;

// if (defaultValue == null)
// throw new Exception($"Unsupported variable type: {boundExpression.Type}");

// _variables[name] = defaultValue;
// return new BoundAssignmentExpression(name, boundExpression);
// a=1
var name = syntax.IdentifierToken.Text;
var boundExpression = BindExpression(syntax.Expression);
var defaultValue =
boundExpression.Type == typeof(int)
? (object)0
: boundExpression.Type == typeof(bool)
? (object)false
: null;

if (defaultValue == null)
throw new Exception($"Unsupported variable type: {boundExpression.Type}");

_variables[name] = defaultValue;
return new BoundAssignmentExpression(name, boundExpression);

var existingVariable = _variables.Keys.FirstOrDefault(v => v.Name == name);
if (existingVariable != null)
_variables.Remove(existingVariable);

var variable = new VariableSymbol(name, boundExpression.Type);
_variables[variable] = null;

return new BoundAssignmentExpression(variable, boundExpression);

}

private BoundExpression BindNameExpression(NameExpressionSyntax syntax)
{
// var name = syntax.IdentifierToken.Text;
// if (!_variables.TryGetValue(name, out var value))
// {
// _diagnostics.ReportUndefinedName(syntax.IdentifierToken.Span, name);
// return new BoundLiteralExpression(0);
// }

// var type = value.GetType();
// return new BoundVariableExpression(name, type);
var name = syntax.IdentifierToken.Text;
if (!_variables.TryGetValue(name, out var value))
var variable = _variables.Keys.FirstOrDefault(v => v.Name == name);

if (variable == null)
{
_diagnostics.ReportUndefinedName(syntax.IdentifierToken.Span, name);
return new BoundLiteralExpression(0);
}

var type = value.GetType();
return new BoundVariableExpression(name, type);
return new BoundVariableExpression(variable);
}

private BoundExpression BindLiteralExpression(LiteralExpressionSyntax syntax)
Expand Down
8 changes: 4 additions & 4 deletions Minsk/CodeAnalysis/Binding/BoundAssignmentExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace Minsk.CodeAnalysis.Binding
{
internal sealed class BoundAssignmentExpression : BoundExpression
{
public BoundAssignmentExpression(string name, BoundExpression expression)
public BoundAssignmentExpression(VariableSymbol variable, BoundExpression expression)
{
Name = name;
Variable = variable;
Expression = expression;
}

public override BoundNodeKind Kind => BoundNodeKind.AssignmentExpression;
public override BoundNodeKind Kind => BoundNodeKind.BoundAssignmentExpression;
public override Type Type => Expression.Type;
public string Name { get; }
public VariableSymbol Variable { get; }
public BoundExpression Expression { get; }
}
}
2 changes: 1 addition & 1 deletion Minsk/CodeAnalysis/Binding/BoundBinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public BoundBinaryExpression(BoundExpression left, BoundBinaryOperator op, Bound
Right = right;
}

public override BoundNodeKind Kind => BoundNodeKind.BinaryExpression;
public override BoundNodeKind Kind => BoundNodeKind.BoundBinaryExpression;
public override Type Type => Op.LeftType;
public BoundExpression Left { get; }
public BoundBinaryOperator Op { get; }
Expand Down
2 changes: 1 addition & 1 deletion Minsk/CodeAnalysis/Binding/BoundLiteralExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public BoundLiteralExpression(object value)
Value = value;
}

public override BoundNodeKind Kind => BoundNodeKind.LiteralExpression;
public override BoundNodeKind Kind => BoundNodeKind.BoundLiteralExpression;
public override Type Type => Value.GetType();
public object Value { get; }
}
Expand Down
10 changes: 5 additions & 5 deletions Minsk/CodeAnalysis/Binding/BoundNodeKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Minsk.CodeAnalysis.Binding
{
internal enum BoundNodeKind
{
LiteralExpression,
VariableExpression,
AssignmentExpression,
UnaryExpression,
BinaryExpression,
BoundLiteralExpression,
BoundVariableExpression,
BoundAssignmentExpression,
BoundUnaryExpression,
BoundBinaryExpression,
}
}
2 changes: 1 addition & 1 deletion Minsk/CodeAnalysis/Binding/BoundUnaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand)
Op = op;
}

public override BoundNodeKind Kind => BoundNodeKind.UnaryExpression;
public override BoundNodeKind Kind => BoundNodeKind.BoundUnaryExpression;
public override Type Type => Op.OperandType;
public BoundUnaryOperator Op { get; }
public BoundExpression Operand { get; }
Expand Down
11 changes: 5 additions & 6 deletions Minsk/CodeAnalysis/Binding/BoundVariableExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ namespace Minsk.CodeAnalysis.Binding
{
internal sealed class BoundVariableExpression: BoundExpression
{
public BoundVariableExpression(string name, Type type)
public BoundVariableExpression(VariableSymbol variable)
{
Name = name;
Type = type;
Variable = variable;
}

public override BoundNodeKind Kind => BoundNodeKind.VariableExpression;
public string Name { get; }
public override Type Type { get; }
public override BoundNodeKind Kind => BoundNodeKind.BoundVariableExpression;
public override Type Type => Variable.Type;
public VariableSymbol Variable { get; }
}
}
44 changes: 44 additions & 0 deletions Minsk/CodeAnalysis/Compilation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Minsk.CodeAnalysis.Binding;
using Minsk.CodeAnalysis.Syntax;

namespace Minsk.CodeAnalysis
{
public sealed class Compilation
{
public Compilation(SyntaxTree syntaxTree)
{
SyntaxTree = syntaxTree;
}

public SyntaxTree SyntaxTree { get; }

public EvaluateResult evaluate(Dictionary<VariableSymbol,object> variables){
// var binder = new Binder(variables);
// var boundExpression = binder.BindExpression(SyntaxTree.Root);
// var diagnostics = SyntaxTree.Diagnostics.Concat(binder.Diagnostics).ToArray();

// if (diagnostics.Any())
// {
// return new EvaluateResult(diagnostics,null);
// }

// var value = new Evaluator(boundExpression,variables).Evaluate();
// return new EvaluateResult(Array.Empty<Diagnostic>(),value);
var binder = new Binder(variables);
var boundExpression = binder.BindExpression(SyntaxTree.Root);

var diagnostics = SyntaxTree.Diagnostics.Concat(binder.Diagnostics).ToImmutableArray();
if (diagnostics.Any())
return new EvaluateResult(diagnostics, null);

var evaluator = new Evaluator(boundExpression, variables);
var value = evaluator.Evaluate();
return new EvaluateResult(ImmutableArray<Diagnostic>.Empty, value);
}
}
}

33 changes: 0 additions & 33 deletions Minsk/CodeAnalysis/Complication.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Minsk/CodeAnalysis/Diagnostic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

using Minsk.CodeAnalysis.Text;
/**
diagnostic目的是当输入的代码与语法规则不合的时候指出错误,
之前的diagnostic都是大致提示,比如` operator xxx is not defined for xtype and ytype`
Expand Down
9 changes: 8 additions & 1 deletion Minsk/CodeAnalysis/DiagnosticBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using Minsk.CodeAnalysis.Syntax;

using Minsk.CodeAnalysis.Text;

namespace Minsk.CodeAnalysis
{
Expand Down Expand Up @@ -57,6 +57,13 @@ public void ReportUndefinedBinaryOperator(TextSpan span, string operatorText, Ty
Report(span, message);
}

public void ReportBadCharacter(int position, char character)
{
var span = new TextSpan(position, 1);
var message = $"Bad character input: '{character}'.";
Report(span, message);
}

public void ReportUndefinedName(TextSpan span, string name)
{
var message = $"Variable '{name}' doesn't exist.";
Expand Down
Loading