Skip to content

Commit

Permalink
Merge pull request #3665 from maxonfjvipon/fix/#3655/inline-voids
Browse files Browse the repository at this point in the history
fix(#3651): add inline voids to phi
  • Loading branch information
yegor256 authored Dec 13, 2024
2 parents 714cdfc + b60b15f commit efd9735
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ void convertsToXmirAndBack(final String pack, @Mktmp final Path temp) throws Exc
final long saved = temp.resolve(path).toFile().lastModified();
final FakeMaven maven = new FakeMaven(temp).execute(UnphiMojo.class);
final Path xmir = temp.resolve(String.format("target/%s/main.xmir", ParseMojo.DIR));
Logger.debug(this, "Unphied XMIR: %s", new TextOf(xmir).asString());
maven.foreignTojos().add("name").withXmir(xmir);
final Path result = maven
.execute(OptimizeMojo.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2024 Objectionary.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
---
asserts:
- /program/objects/o[@name='main' and not(@base) and o[@name='x'] and o[@name='a' and @base='∅']]
phi: |-
{
main(a) ↦ ⟦
x ↦ ξ.y
}
10 changes: 10 additions & 0 deletions eo-parser/src/main/antlr4/org/eolang/parser/Phi.g4
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ binding

tauBinding
: attribute ARROW object
| attribute voids ARROW formation
;

voids
: LB (void? | void (COMMA void)+) RB
;

void: LABEL
| ALPHA
| PHI
;

attribute
Expand Down
12 changes: 0 additions & 12 deletions eo-parser/src/main/java/org/eolang/parser/Objects.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ interface Objects extends Iterable<Directive> {
*/
Objects start(int line, int pos);

/**
* Start new object without position.
* @return Self.
*/
Objects start();

/**
* Add data.
* @param data Data.
Expand Down Expand Up @@ -106,12 +100,6 @@ public Objects start(final int line, final int pos) {
return this.prop("line", line).prop("pos", pos);
}

@Override
public Objects start() {
this.dirs.add("o");
return this;
}

@Override
public Objects data(final String data) {
this.dirs.set(data);
Expand Down
67 changes: 56 additions & 11 deletions eo-parser/src/main/java/org/eolang/parser/XePhiListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void enterProgram(final PhiParser.ProgramContext ctx) {
.append(new DrListing(ctx))
.xpath("/program").strict(1);
if (ctx.object() == null || ctx.object().formation() == null) {
this.objects().start();
this.startObject(ctx);
}
}

Expand Down Expand Up @@ -179,9 +179,11 @@ public void enterScoped(final PhiParser.ScopedContext ctx) {
if (ctx.HOME() != null) {
this.objects().prop("base", "Q");
} else if (ctx.DEF_PACKAGE() != null) {
final int line = ctx.getStart().getLine();
final int pos = ctx.getStart().getCharPositionInLine();
this.objects().prop("base", "Q").leave()
.start().prop("base", ".org").prop("method").leave()
.start().prop("base", ".eolang").prop("method");
.start(line, pos + 1).prop("base", ".org").prop("method").leave()
.start(line, pos + 5).prop("base", ".eolang").prop("method");
} else {
this.objects().prop("base", "$");
}
Expand Down Expand Up @@ -221,14 +223,42 @@ public void exitBinding(final PhiParser.BindingContext ctx) {

@Override
public void enterTauBinding(final PhiParser.TauBindingContext ctx) {
this.enterObjectBinding();
this.startObject(ctx);
}

@Override
public void exitTauBinding(final PhiParser.TauBindingContext ctx) {
this.exitObjectBinding();
}

@Override
public void enterVoids(final PhiParser.VoidsContext ctx) {
// Nothing here
}

@Override
public void exitVoids(final PhiParser.VoidsContext ctx) {
// Nothing here
}

@Override
public void enterVoid(final PhiParser.VoidContext ctx) {
final String nme;
if (ctx.PHI() != null) {
nme = "@";
} else if (ctx.LABEL() != null || ctx.ALPHA() != null) {
nme = ctx.getText();
} else {
nme = "";
}
this.addVoidAttribute(ctx).prop("name", nme);
}

@Override
public void exitVoid(final PhiParser.VoidContext ctx) {
this.objects().leave();
}

@Override
@SuppressWarnings("PMD.ConfusingTernary")
public void enterAttribute(final PhiParser.AttributeContext ctx) {
Expand All @@ -252,7 +282,7 @@ public void exitAttribute(final PhiParser.AttributeContext ctx) {

@Override
public void enterEmptyBinding(final PhiParser.EmptyBindingContext ctx) {
this.enterObjectBinding().prop("base", "∅");
this.addVoidAttribute(ctx);
}

@Override
Expand Down Expand Up @@ -329,7 +359,7 @@ public void exitApplicationObjects(final PhiParser.ApplicationObjectsContext ctx

@Override
public void enterJustObject(final PhiParser.JustObjectContext ctx) {
this.enterObjectBinding();
this.startObject(ctx);
final int index = this.alphas.peek();
this.alphas.pop();
this.alphas.push(index + 1);
Expand All @@ -344,7 +374,7 @@ public void exitJustObject(final PhiParser.JustObjectContext ctx) {
@Override
@SuppressWarnings("PMD.ConfusingTernary")
public void enterDispatch(final PhiParser.DispatchContext ctx) {
this.objects().start().prop("method");
this.startObject(ctx).prop("method");
}

@Override
Expand Down Expand Up @@ -399,7 +429,10 @@ public void enterData(final PhiParser.DataContext ctx) {
}
this.objects()
.prop("base", base)
.start()
.start(
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine() + base.length() + 1
)
.prop("base", "org.eolang.bytes")
.data(data.get())
.leave();
Expand Down Expand Up @@ -444,11 +477,23 @@ private Objects objects() {
}

/**
* Enter either tau or empty binding.
* Adds void attribute to the current object.
* @param ctx Context
* @return Objects
*/
private Objects addVoidAttribute(final ParserRuleContext ctx) {
return this.startObject(ctx).prop("base", "∅");
}

/**
* Start object.
* @param ctx Parser rule context.
* @return Objects
*/
private Objects enterObjectBinding() {
return this.objects().start();
private Objects startObject(final ParserRuleContext ctx) {
return this.objects().start(
ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine()
);
}

/**
Expand Down
Loading

0 comments on commit efd9735

Please sign in to comment.