Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Feb 25, 2022
1 parent b8cd6a6 commit 1e14e52
Show file tree
Hide file tree
Showing 38 changed files with 667 additions and 491 deletions.
38 changes: 22 additions & 16 deletions kscr-compiler/Class/ClassCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using KScr.Compiler.Code;
using KScr.Compiler.Code;
using KScr.Lib;
using KScr.Lib.Bytecode;
using KScr.Lib.Exception;
Expand All @@ -9,13 +8,13 @@ namespace KScr.Compiler.Class
{
public class ClassCompiler : AbstractCompiler
{
private bool inBody = false;
private MemberModifier? modifier;
private Field field = null!;
private bool inBody;
private string? memberName;
private int memberType = 0;
private ITypeInfo targetType = null!;
private int memberType;
private Method method = null!;
private Field field = null!;
private MemberModifier? modifier;
private ITypeInfo targetType = null!;

public override ICompiler? AcceptToken(RuntimeBase vm, ref CompilerContext ctx)
{
Expand Down Expand Up @@ -95,14 +94,18 @@ public class ClassCompiler : AbstractCompiler
if (!inBody)
break;
if (targetType == null)
{ // is return type
{
// is return type
string targetTypeIdentifier = ctx.Token.Arg!;
targetType = vm.FindTypeInfo(targetTypeIdentifier, ctx.Class, ctx.Package)
targetType = vm.FindTypeInfo(targetTypeIdentifier, ctx.Class, ctx.Package)
?? throw new CompilerException("Could not find type: " + targetTypeIdentifier);
}
else if (memberName == null)
else if (memberName == null)
// is name
{
memberName = ctx.Token.Arg!;
}

memberType = 2; // field
break;
// into field
Expand All @@ -119,9 +122,10 @@ public class ClassCompiler : AbstractCompiler
memberType = 1; // method

// compile parameter definition
method = new Method(ctx.Class, memberName!,
modifier ?? (ctx.Class.ClassType is ClassType.Interface or ClassType.Annotation
? MemberModifier.Public : MemberModifier.Protected));
method = new Method(ctx.Class, memberName!,
modifier ?? (ctx.Class.ClassType is ClassType.Interface or ClassType.Annotation
? MemberModifier.Public
: MemberModifier.Protected));
ctx = new CompilerContext(ctx, CompilerType.ParameterDefintion);
ctx.TokenIndex += 1;
CompilerLoop(vm, new ParameterDefinitionCompiler(this, method), ref ctx);
Expand All @@ -139,16 +143,18 @@ public class ClassCompiler : AbstractCompiler
ctx.Parent!.TokenIndex = ctx.TokenIndex;
ctx = ctx.Parent!;
}
break;

break;
case TokenType.ParAccOpen:
if (!inBody)
{
inBody = true;
break;
}

if (method == null)
break;

// compile method body
ctx = new CompilerContext(ctx, CompilerType.CodeStatement);
ctx.TokenIndex += 1;
Expand All @@ -157,7 +163,7 @@ public class ClassCompiler : AbstractCompiler
ctx.Class.DeclaredMembers[memberName!] = method;
ctx.Parent!.TokenIndex = ctx.TokenIndex;
ctx = ctx.Parent!;

break;
}

Expand Down
21 changes: 13 additions & 8 deletions kscr-compiler/Class/ParameterDefinitionCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using KScr.Lib;
using KScr.Lib;
using KScr.Lib.Bytecode;
using KScr.Lib.Exception;
using KScr.Lib.Model;
Expand All @@ -9,33 +8,38 @@ namespace KScr.Compiler.Class
public class ParameterDefinitionCompiler : AbstractCompiler
{
private readonly Method _method;
private int pIndex = -1, pState = 0;
private bool _active = true;
public override bool Active => _active;
private int pIndex = -1, pState;

public ParameterDefinitionCompiler(ClassCompiler parent, Method method) : base(parent)
{
_method = method;
}

public override bool Active => _active;

public override ICompiler? AcceptToken(RuntimeBase vm, ref CompilerContext ctx)
{
switch (ctx.Token.Type)
{
case TokenType.Word:
if (pState == 0)
{ // parse type
{
// parse type
if (pIndex >= _method.Parameters.Count)
throw new CompilerException("Invalid parameter index during compilation");

_method.Parameters.Add(new MethodParameter());
_method.Parameters[++pIndex].Type = vm.FindType(ctx.Token.Arg!)!;
pState = 1;
} else if (pState == 1)
{ // parse name
}
else if (pState == 1)
{
// parse name
_method.Parameters[pIndex].Name = ctx.Token.Arg!;
pState = 0;
}

break;
case TokenType.Comma:
pIndex += 1;
Expand All @@ -46,6 +50,7 @@ public ParameterDefinitionCompiler(ClassCompiler parent, Method method) : base(p
break;
default: throw new CompilerException("Unexpected token: " + ctx.Token);
}

return this;
}
}
Expand Down
15 changes: 10 additions & 5 deletions kscr-compiler/Class/TypeParameterDefinitionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,37 @@ namespace KScr.Compiler.Class
public class TypeParameterDefinitionCompiler : AbstractCompiler
{
private readonly IClass _class;
private int pIndex = -1, pState = 0;
private bool _active = true;
public override bool Active => _active;
private int pIndex = -1, pState;

public TypeParameterDefinitionCompiler(ClassCompiler parent, IClass @class) : base(parent)
{
_class = @class;
}

public override bool Active => _active;

public override ICompiler? AcceptToken(RuntimeBase vm, ref CompilerContext ctx)
{
switch (ctx.Token.Type)
{
case TokenType.Word:
if (pState == 0)
{ // parse name
{
// parse name
if (pIndex >= _class.TypeParameters.Count)
throw new CompilerException("Invalid TypeParameter index during compilation");

_class.TypeParameters.Add(new TypeParameter(ctx.Token.Arg!));
pIndex += 1;
pState += 1;
}
else
{ // parse spec type name
{
// parse spec type name
// todo
}

break;
case TokenType.Comma:
pState = 0;
Expand All @@ -61,6 +65,7 @@ public TypeParameterDefinitionCompiler(ClassCompiler parent, IClass @class) : ba
return Parent;
default: throw new CompilerException("Unexpected token: " + ctx.Token);
}

return this;
}
}
Expand Down
19 changes: 11 additions & 8 deletions kscr-compiler/Code/CodeCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using KScr.Compiler.Class;
using KScr.Lib;
using KScr.Lib.Bytecode;
using KScr.Lib.Core;
using KScr.Lib.Exception;
using KScr.Lib.Model;
using KScr.Lib.Store;
Expand All @@ -13,12 +11,12 @@ public abstract class AbstractCodeCompiler : AbstractCompiler
{
protected bool _active = true;

public override bool Active => _active;

protected AbstractCodeCompiler(ICompiler parent) : base(parent)
{
}

public override bool Active => _active;

public override ICompiler? AcceptToken(RuntimeBase vm, ref CompilerContext ctx)
{
CompilerContext subctx;
Expand Down Expand Up @@ -74,7 +72,8 @@ protected AbstractCodeCompiler(ICompiler parent) : base(parent)
ctx.Component = new StatementComponent
{
Type = ctx.Statement.Type == StatementComponentType.Declaration
? StatementComponentType.Declaration : StatementComponentType.Provider,
? StatementComponentType.Declaration
: StatementComponentType.Provider,
CodeType = BytecodeType.ExpressionVariable,
Arg = ctx.Token.Arg!
};
Expand Down Expand Up @@ -117,7 +116,8 @@ protected AbstractCodeCompiler(ICompiler parent) : base(parent)
// pipe operands
case TokenType.ParDiamondOpen:
if (ctx.NextToken!.Type == TokenType.ParDiamondOpen)
{ // compile Emitter
{
// compile Emitter
ctx.Component = new StatementComponent
{
Type = StatementComponentType.Emitter
Expand All @@ -134,10 +134,12 @@ protected AbstractCodeCompiler(ICompiler parent) : base(parent)
ctx.LastComponent!.SubStatement = subctx.Statement;
ctx.TokenIndex = subctx.TokenIndex;
}

break;
case TokenType.ParDiamondClose:
if (ctx.NextToken!.Type == TokenType.ParDiamondClose)
{ // compile Emitter
{
// compile Emitter
ctx.Component = new StatementComponent
{
Type = StatementComponentType.Consumer
Expand All @@ -149,6 +151,7 @@ protected AbstractCodeCompiler(ICompiler parent) : base(parent)
ctx.LastComponent!.SubStatement = subctx.Statement;
ctx.TokenIndex = subctx.TokenIndex;
}

break;
case TokenType.Terminator:
ctx.Statement = new Statement();
Expand All @@ -167,7 +170,7 @@ private static void CompileDeclaration(CompilerContext ctx, IClassInstance targe
{
Type = StatementComponentType.Declaration,
TargetType = targetType
};/*
}; /*
ctx.Component = new StatementComponent
{
Type = StatementComponentType.Declaration,
Expand Down
11 changes: 7 additions & 4 deletions kscr-compiler/Code/ExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class ExpressionCompiler : AbstractCodeCompiler
private readonly bool _endBeforeTerminator;
private readonly TokenType[] _terminators;

public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false, params TokenType[] terminators) : base(parent)
public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false, params TokenType[] terminators) :
base(parent)
{
_endBeforeTerminator = endBeforeTerminator;
_terminators = terminators;
}

public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false, TokenType terminator = TokenType.Terminator)
: this(parent, endBeforeTerminator, new[]{terminator})
public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false,
TokenType terminator = TokenType.Terminator)
: this(parent, endBeforeTerminator, new[] { terminator })
{
}

Expand Down Expand Up @@ -95,7 +97,7 @@ public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false, To
};
break;
}

var use = base.AcceptToken(vm, ref ctx);
if (_terminators.Contains(ctx.NextToken?.Type ?? TokenType.Terminator))
{
Expand All @@ -104,6 +106,7 @@ public ExpressionCompiler(ICompiler parent, bool endBeforeTerminator = false, To
_active = false;
return _endBeforeTerminator ? Parent : this;
}

return use;
}
}
Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/Code/ParameterExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ParameterExpressionCompiler(ICompiler parent) : base(parent)

while (ctx.Token.Type != TokenType.ParRoundClose)
{
CompilerContext subctx = new CompilerContext(ctx, CompilerType.CodeParameterExpression);
var subctx = new CompilerContext(ctx, CompilerType.CodeParameterExpression);
CompilerLoop(vm, new ExpressionCompiler(this, false, TokenType.Comma), ref subctx);
mpc.Expressions.Add(subctx.Component);
ctx.TokenIndex = subctx.TokenIndex;
Expand Down
6 changes: 3 additions & 3 deletions kscr-compiler/Code/StatementCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using KScr.Lib;
using KScr.Lib.Bytecode;
using KScr.Lib.Exception;
using KScr.Lib.Model;

namespace KScr.Compiler.Code
Expand All @@ -20,7 +19,8 @@ public StatementCompiler(ICompiler parent) : base(parent)
switch (ctx.Token.Type)
{
case TokenType.OperatorEquals:
if (ctx.Statement.Type == StatementComponentType.Declaration || ctx.Component.CodeType == BytecodeType.ExpressionVariable)
if (ctx.Statement.Type == StatementComponentType.Declaration ||
ctx.Component.CodeType == BytecodeType.ExpressionVariable)
{
// assignment
ctx.Component = new StatementComponent
Expand All @@ -46,7 +46,7 @@ public StatementCompiler(ICompiler parent) : base(parent)
_active = false;
return this;
}

return base.AcceptToken(vm, ref ctx);
}
}
Expand Down
8 changes: 3 additions & 5 deletions kscr-compiler/CompilerRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

using System;
using System;
using System.Collections.Generic;
using System.IO;
using KScr.Compiler.Class;
using KScr.Compiler.Code;
using KScr.Lib;
using KScr.Lib.Model;
using KScr.Lib.Store;
Expand All @@ -15,14 +13,14 @@ public class CompilerRuntime : RuntimeBase
public override ObjectStore ObjectStore => null!;
public override ClassStore ClassStore { get; } = new();
public override ITokenizer Tokenizer => new Tokenizer();
public override ClassCompiler Compiler => new ClassCompiler();
public override ClassCompiler Compiler => new();

public void CompileFiles(IEnumerator<FileInfo> files)
{
var compiler = Compiler;
if (!files.MoveNext())
throw new ArgumentException("Missing compiler Classpath");
CompilerContext context = compiler.CompileClass(this, files.Current);
var context = compiler.CompileClass(this, files.Current);
while (files.MoveNext())
compiler.CompileClass(this, files.Current, ref context);
}
Expand Down
4 changes: 2 additions & 2 deletions kscr-compiler/MainCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
using KScr.Compiler.Class;
using KScr.Lib;
using KScr.Lib.Bytecode;
using KScr.Lib.Model;

namespace KScr.Compiler
{
public sealed class CompilerArgs
{
[Option("source", Required = true, HelpText = "The source directory")]
public string SourceDir { get; set; } = null!;

[Option("output", Required = false, HelpText = "The output directory; defaults to ./output/")]
public string OutputDir { get; set; } = string.Empty;
}

public sealed class MainCompiler
{
public static void Compile(RuntimeBase vm, DirectoryInfo sourceDir, DirectoryInfo outputDir)
Expand Down
Loading

0 comments on commit 1e14e52

Please sign in to comment.