Skip to content

Commit

Permalink
add support for iterating over text chars
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvergnaud committed Jul 28, 2022
1 parent 930b0d0 commit 4dc802a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
27 changes: 27 additions & 0 deletions Core/src/main/java/prompto/intrinsic/PromptoString.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prompto.intrinsic;

import java.util.Collection;
import java.util.Iterator;

public abstract class PromptoString {

Expand Down Expand Up @@ -82,6 +83,32 @@ public static long indexOf(String s, String v, long i) {
int r = s.indexOf(v, (int)i -1);
return r + 1;
}

public static Iterable<Character> iterable(String s) {

return new Iterable<Character>() {

@Override
public Iterator<Character> iterator() {

return new Iterator<Character>() {

int i = 0;

@Override
public boolean hasNext() {
return i < s.length();
}

@Override
public Character next() {
return s.charAt(i++);
}

};
}
};
}



Expand Down
11 changes: 8 additions & 3 deletions Core/src/main/java/prompto/statement/ForEachStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import prompto.error.PromptoError;
import prompto.expression.IExpression;
import prompto.grammar.Identifier;
import prompto.intrinsic.PromptoString;
import prompto.parser.ICodeSection;
import prompto.parser.ISection;
import prompto.runtime.BreakResult;
Expand Down Expand Up @@ -341,9 +342,13 @@ private ResultInfo compileWithoutIndex(Context context, MethodInfo method, Flags
}

private StackLocal compileIterator(Context context, MethodInfo method, Flags flags) {
source.compile(context, method, flags);
InterfaceConstant m = new InterfaceConstant(Iterable.class, "iterator", Iterator.class);
method.addInstruction(Opcode.INVOKEINTERFACE, m);
ResultInfo info = source.compile(context, method, flags);
if(info.getType() == String.class) {
MethodConstant mc = new MethodConstant(PromptoString.class, "iterable", String.class, Iterable.class);
method.addInstruction(Opcode.INVOKESTATIC, mc);
}
InterfaceConstant ic = new InterfaceConstant(Iterable.class, "iterator", Iterator.class);
method.addInstruction(Opcode.INVOKEINTERFACE, ic);
String iterName = method.nextTransientName("iter");
StackLocal iterLocal = method.registerLocal(iterName, VerifierType.ITEM_Object, new ClassConstant(Iterator.class));
method.addInstruction(Opcode.ASTORE, new ByteOperand((byte)iterLocal.getIndex()), new ClassConstant(Iterator.class));
Expand Down
6 changes: 6 additions & 0 deletions Core/src/main/java/prompto/type/TextType.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public IType checkSlice(Context context) {
return this;
}

@Override
public IType checkIterator(Context context) {
return CharacterType.instance();
}


@Override
public Set<IMethodDeclaration> getMemberMethods(Context context, Identifier id) throws PromptoError {
switch(id.toString()) {
Expand Down
10 changes: 10 additions & 0 deletions Core/src/main/resources/prompto/intrinsic/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ String.prototype.equals = function(value) {
// use == because === fails in Nashorn
return this == value;
};

String.prototype.iterator = function() {
var s = this;
return {
idx: 0,
hasNext: function() { return this.idx < s.length; },
next: function() { return s[this.idx++]; }
};
};

var intrinsic = {};

if(typeof(window)==="object" && window.document) {
Expand Down

0 comments on commit 4dc802a

Please sign in to comment.