Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
#3 Add FieldDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallimathias committed Jan 9, 2018
1 parent dcf6c4e commit 7dccdd4
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 7 deletions.
1 change: 1 addition & 0 deletions Arrow.Core/Arrow.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="ArrowCompiler.cs" />
<Compile Include="Parsing\Definition\ClassDeclarationSyntax.cs" />
<Compile Include="Parsing\Definition\ComplexTypeSyntax.cs" />
<Compile Include="Parsing\Definition\FieldDeclarationSyntax.cs" />
<Compile Include="Parsing\Definition\IdentifierSyntax.cs" />
<Compile Include="Parsing\Definition\IntegerSyntax.cs" />
<Compile Include="Parsing\Definition\MethodDeclarationSyntax.cs" />
Expand Down
33 changes: 33 additions & 0 deletions Arrow.Core/Parsing/Definition/FieldDeclarationSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arrow.Core.Parsing.Definition
{
[Syntax(SyntaxDefinitionType.FieldDeclaration)]
public class FieldDeclarationSyntax : VariableDeclarationSyntax
{
public FieldDeclarationSyntax() : base(nameof(FieldDeclarationSyntax))
{

}

public override bool TryParse(SyntaxStream stream, Scanner scanner)
{
for (int i = 0; i < stream.Count; i++)
{
if (stream[i].Name == "CodeLineEnd")
{
var result = base.TryParse(stream.Take(i), scanner);
Length += 1;
return result;
}
}

return false;

}
}
}
23 changes: 19 additions & 4 deletions Arrow.Core/Parsing/Definition/VariableDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ namespace Arrow.Core.Parsing.Definition
[Syntax(SyntaxDefinitionType.Variable)]
public class VariableDeclarationSyntax : Syntax
{
public Syntax Expression { get; protected set; }

public TypeDeclarationSyntax TypeDeclarationSyntax { get; set; }

public VariableDeclarationSyntax() : base(nameof(VariableDeclarationSyntax))
{
}

public Syntax Expression { get; private set; }
protected VariableDeclarationSyntax(string name) : base(name)
{

}

public override bool TryParse(SyntaxStream stream, Scanner scanner)
{
var result = false;

if (stream.Count < 2)
return false;

if (stream[0].Name == "Var" && stream[1].Name == "Identifier")
{
var identifierToken = (TokenSyntax)stream[1];
Expand All @@ -31,9 +40,15 @@ public override bool TryParse(SyntaxStream stream, Scanner scanner)
result = true;
}

if ( stream.Count > 2 &&stream[2].Name == "AssignEquals")
if(stream.Count > Length && scanner.TryScan(stream.Skip(Length),out TypeDeclarationSyntax type))
{
TypeDeclarationSyntax = type;
Length += type.Length;
}

if ( stream.Count > Length &&stream[Length].Name == "AssignEquals")
{
Expression = scanner.Scan(stream.Skip(3));
Expression = scanner.Scan(stream.Skip(Length + 1));
Length += 1 + Expression.Length;
}

Expand Down
4 changes: 3 additions & 1 deletion Arrow.Core/Parsing/SyntaxDefinitionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public enum SyntaxDefinitionType
Scope,
Primitive,
TypeDeclaration,
FieldDeclaration,
MethodDeclaration,
ClassDeclaration,
NamespaceDeclaration
NamespaceDeclaration,

}
}
3 changes: 3 additions & 0 deletions Arrow.Core/Visitors/ClassScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ public class ClassScope : Scope
{
public readonly TypeBuilder TypeBuilder;

public Dictionary<string,FieldBuilder> Fields;

public ClassScope(TypeBuilder typeBuilder)
{
Fields = new Dictionary<string, FieldBuilder>();
TypeBuilder = typeBuilder;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Arrow.Core/Visitors/ClassVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public void Visit(ScopeSyntax syntax, ClassScope scope)
}
}

public void Visit(FieldDeclarationSyntax syntax, ClassScope scope)
{
var field = scope.TypeBuilder.DefineField(syntax.Name, syntax.TypeDeclarationSyntax.TypeSyntax.Type, System.Reflection.FieldAttributes.Private);
scope.Fields.Add(syntax.Name, field);
}

public void Visit(MethodDeclarationSyntax syntax, ClassScope scope)
{
var parameterTypes = syntax.Signature?.Parameters.Select(p => p.TypeDeclaration.TypeSyntax.Type).ToArray();
Expand Down
2 changes: 2 additions & 0 deletions CompilerTests/CompilerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -49,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ClassTests.cs" />
<Compile Include="FieldTests.cs" />
<Compile Include="FileTests.cs" />
<Compile Include="MethodTests.cs" />
<Compile Include="NewLineTests.cs" />
Expand Down
30 changes: 30 additions & 0 deletions CompilerTests/FieldTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Arrow.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompilerTests
{
[TestClass]
public class FieldTests
{
private ArrowCompiler compiler;

public FieldTests()
{
compiler = new ArrowCompiler();
}

[TestMethod]
public void SimpleFieldTest()
{
compiler.GetAssembly(
@"class test {
var global : int;
}");
}
}
}
2 changes: 0 additions & 2 deletions CompilerTests/FileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public FileTests()
[TestMethod]
public void FileCompiling()
{
//TODO: an die Zunft diesen Fehler bzw. Test bitte lösen Gruß Vergangenheits Lassi und Marcus

compiler.GetAssembly(File.ReadAllText(@"D:\Projekte\Visual 2017\TheLanguage\CompilerTests\SimpleTest.arrow"));
}
}
Expand Down
2 changes: 2 additions & 0 deletions CompilerTests/SimpleTest.arrow
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Math
{
var det : int;

def Add(base : int, value : int) : int
{
ret base + value;
Expand Down

0 comments on commit 7dccdd4

Please sign in to comment.