Skip to content

Commit

Permalink
Merge pull request #32 from comroid-git/super-ctor-calls
Browse files Browse the repository at this point in the history
Handle Super-Constructor calls
  • Loading branch information
burdoto authored Oct 19, 2022
2 parents da3b479 + 57fad33 commit 2b771bd
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 304 deletions.
2 changes: 2 additions & 0 deletions examples/PrintNumbers.kscr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.comroid.kscr.test;

import org.comroid.kscr.core.Throwable;
import org.comroid.kscr.core.Sequencable;
import org.comroid.kscr.time.DateTime;

public class PrintNumbers implements Throwable {
public static int StackSize => 64;
Expand All @@ -23,6 +24,7 @@ public class PrintNumbers implements Throwable {
// stdio <<- "first argument is:" + args[0] + endl
// <<- "args array is " + args + endl;
public static void main() {
DateTime datetime = new DateTime(3, 4);

int[] xy = new int[6];
stdio <<- "array size is " + xy.length() + endl;
Expand Down
2 changes: 1 addition & 1 deletion grammar/KScrParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ indexerDecl: LSQUAR type idPart (COMMA type idPart)* RSQUAR;
indexerExpr: LSQUAR expr (COMMA expr)* RSQUAR;
cast: LPAREN type COLON expr RPAREN;
declaration: type idPart (ASSIGN expr)?;
mutation: binaryop? ASSIGN expr;
mutation: (binaryop | binaryop_late)? ASSIGN expr;
call: idPart arguments;
ctorCall: NEW type arguments;
newArray: NEW type indexerExpr;
Expand Down
7 changes: 7 additions & 0 deletions kscr-bytecode/Adapter/BytecodeAdapterV0_10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ protected override void WriteMethod(Stream stream, StringCache strings, Method m
Write(stream, strings, mtd.ReturnType.FullDetailedName);
Write(stream, strings, mtd.SourceLocation);
Write(stream, strings, mtd.Parameters.ToArray());
if (mtd.Name == Method.ConstructorName)
Write(stream, strings, mtd.SuperCalls.ToArray());
Write(stream, strings, mtd.Body);
}

Expand All @@ -158,9 +160,14 @@ protected override Method ReadMethod(RuntimeBase vm, Stream stream, StringCache
returnType = vm.FindType(ReadString(stream, strings));
srcPos = Load<SourcefilePosition>(vm, strings, stream, pkg, cls);
var parameters = ReadArray<MethodParameter>(vm, stream, strings, pkg, cls);
StatementComponent[]? supers = name == Method.ConstructorName
? ReadArray<StatementComponent>(vm, stream, strings, pkg, cls)
: null;
var body = Load<ExecutableCode>(vm, strings, stream, pkg, cls);
var mtd = new Method(srcPos, cls!, name, returnType, mod) { Body = body };
mtd.Parameters.AddRange(parameters);
if (supers != null)
mtd.SuperCalls.AddRange(supers);
return mtd;
}

Expand Down
17 changes: 2 additions & 15 deletions kscr-compiler/KScr/Compiler/AbstractVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace KScr.Compiler;

public abstract class AbstractVisitor<T> : KScrParserBaseVisitor<T>
{
protected AbstractVisitor(RuntimeBase vm, CompilerContext ctx)
protected AbstractVisitor(CompilerRuntime vm, CompilerContext ctx)
{
this.vm = vm;
this.ctx = ctx;
}

protected RuntimeBase vm { get; }
protected CompilerRuntime vm { get; }
protected CompilerContext ctx { get; }
protected ITypeInfo? RequestedType { get; init; }

Expand Down Expand Up @@ -72,19 +72,6 @@ protected TypeParameter VisitTypeParameter(KScrParser.GenericTypeDefContext gtd)
return new TypeParameter(name, spec, target.AsClassInstance(vm)) { DefaultValue = def };
}

protected IClassMember VisitClassMember(KScrParser.MemberContext member)
{
return member.RuleIndex switch
{
KScrParser.RULE_methodDecl or KScrParser.RULE_constructorDecl or KScrParser.RULE_initDecl
or KScrParser.RULE_propertyDecl or KScrParser.RULE_member
=> new ClassMemberVisitor(vm, ctx).Visit(member),
KScrParser.RULE_classDecl => new ClassVisitor(vm, ctx).Visit(member),
_ => throw new ArgumentOutOfRangeException(nameof(member.RuleIndex), member.RuleIndex,
"Invalid Rule for member: " + member)
};
}

protected ExecutableCode VisitCode(ParserRuleContext? code)
{
return code == null ? new ExecutableCode() : new CodeblockVisitor(vm, ctx).Visit(code);
Expand Down
123 changes: 0 additions & 123 deletions kscr-compiler/KScr/Compiler/Class/ClassMemberVisitor.cs

This file was deleted.

33 changes: 1 addition & 32 deletions kscr-compiler/KScr/Compiler/Class/ClassVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace KScr.Compiler.Class;

public class ClassInfoVisitor : AbstractVisitor<ClassInfo>
{
public ClassInfoVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
public ClassInfoVisitor(CompilerRuntime vm, CompilerContext ctx) : base(vm, ctx)
{
}

Expand All @@ -26,37 +26,6 @@ public override ClassInfo VisitClassDecl(KScrParser.ClassDeclContext context)
}
}

public class ClassVisitor : AbstractVisitor<Core.Std.Class>
{
public ClassVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
{
}

private Core.Std.Class cls => ctx.Class!.AsClass(vm);

public override Core.Std.Class VisitClassDecl(KScrParser.ClassDeclContext context)
{
if (context.genericTypeDefs() is { } defs)
foreach (var genTypeDef in defs.genericTypeDef())
if (cls.TypeParameters.All(x => x.Name != genTypeDef.idPart().GetText()))
cls.TypeParameters.Add(VisitTypeParameter(genTypeDef));
if (context.objectExtends() is { } ext)
foreach (var extendsType in ext.type())
cls.DeclaredSuperclasses.Add(VisitTypeInfo(extendsType).AsClassInstance(vm));
if (context.objectImplements() is { } impl)
foreach (var implementsType in impl.type())
cls.DeclaredInterfaces.Add(VisitTypeInfo(implementsType).AsClassInstance(vm));

foreach (var each in context.member())
{
var member = VisitClassMember(each);
cls.DeclaredMembers[member.Name] = member;
}

return cls;
}
}

public class ModifierVisitor : KScrParserBaseVisitor<MemberModifier>
{
public override MemberModifier VisitModPublic(KScrParser.ModPublicContext context)
Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/KScr/Compiler/Code/CodeblockVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace KScr.Compiler.Code;

public class CodeblockVisitor : AbstractVisitor<ExecutableCode>
{
public CodeblockVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
public CodeblockVisitor(CompilerRuntime vm, CompilerContext ctx) : base(vm, ctx)
{
}

Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/KScr/Compiler/Code/ExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace KScr.Compiler.Code;

public class ExpressionVisitor : AbstractVisitor<StatementComponent>
{
public ExpressionVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
public ExpressionVisitor(CompilerRuntime vm, CompilerContext ctx) : base(vm, ctx)
{
}

Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/KScr/Compiler/Code/StatementVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace KScr.Compiler.Code;

public class StatementVisitor : AbstractVisitor<Statement>
{
public StatementVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
public StatementVisitor(CompilerRuntime vm, CompilerContext ctx) : base(vm, ctx)
{
}

Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/KScr/Compiler/Code/TypeInfoVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace KScr.Compiler.Code;

public class TypeInfoVisitor : AbstractVisitor<ITypeInfo>
{
public TypeInfoVisitor(RuntimeBase vm, CompilerContext ctx) : base(vm, ctx)
public TypeInfoVisitor(CompilerRuntime vm, CompilerContext ctx) : base(vm, ctx)
{
}

Expand Down
2 changes: 1 addition & 1 deletion kscr-compiler/KScr/Compiler/CompilerRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void CompileSource(string source, string? basePackage = null)
var decl = MakeFileDecl(src);
ctx = new CompilerContext
{ Parent = ctx, Class = FindClassInfo(src), Imports = FindClassImports(decl.imports()) };
node = SourceNode.ForBaseClass(this, ctx, src, new PackageNode(this, ctx, src.DirectoryName!, pkg));
node = new FileNode(this, ctx, new PackageNode(this, ctx, src.DirectoryName!, pkg), src).CreateClassNode();
var mc = (node as MemberNode)!.ReadMembers();
Debug.WriteLine($"[NodeCompiler] Loaded {mc} members");
}
Expand Down
Loading

0 comments on commit 2b771bd

Please sign in to comment.