Skip to content

Commit 457ca70

Browse files
committed
Implement out_array hard coding
1 parent 7d3fd86 commit 457ca70

11 files changed

Lines changed: 120 additions & 28 deletions

File tree

.idea/workspace.xml

Lines changed: 23 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ts/ClassEntry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ClassEntry implements Scope {
2323
public TypeEntry superClass = null;
2424
public boolean isConsolidated = false;
2525
private final TinyRustSymbolTable root;
26-
private ClassEntry superClassLink;
26+
protected ClassEntry superClassLink;
2727
@SerializeClass({TS, AST}) public TinyRustLexerToken name;
2828
@SerializeClass(TS) public final boolean isExtendable;
2929
@SerializeClass(TS) public final boolean isArrayValid;
@@ -219,21 +219,21 @@ private void checkIfMethodNameClash(TinyRustLexerToken name) {
219219
throw new MethodRedeclaredError(name.location(), "Method " + name.lexeme() + " already exists in class");
220220
}
221221

222-
private void consolidateFields() {
222+
protected void consolidateFields() {
223223
List<VariableEntry> allFields = Utils.concat(superClassLink.fields, fields);
224224
IntStream.range(0, fields.size()).forEach(i -> fields.get(i).index = superClassLink.fields.size() + i); // Set index of fields
225225
List<VariableEntry> repeatedFields = Utils.getRepeatedAttributeNames(allFields);
226226
if (!repeatedFields.isEmpty()) throw new FieldRedeclaredError(repeatedFields);
227227
fields.forEach(VariableEntry::consolidate);
228228
}
229229

230-
private void consolidateMethods() {
230+
protected void consolidateMethods() {
231231
checkDuplicateMethodNames();
232232
checkOverwriting();
233233
methods.forEach(MethodEntry::consolidate);
234234
}
235235

236-
private void checkOverwriting() {
236+
protected void checkOverwriting() {
237237
List<MethodEntry> overrideCandidates = getOverrideCandidates();
238238
overrideCandidates.forEach(this::overrideMethod);
239239
List<MethodEntry> notOverwrittenMethods = Utils.minus(methods, overrideCandidates, method -> method.name.lexeme());
@@ -268,7 +268,7 @@ private Set<MethodEntry> getAllMethods() {
268268
return Stream.concat(superClassLink.getAllMethods().stream(), methods.stream()).collect(Collectors.toSet());
269269
}
270270

271-
private void checkDuplicateMethodNames() {
271+
protected void checkDuplicateMethodNames() {
272272
List<MethodEntry> repeated = Utils.getRepeatedAttributeNames(methods);
273273
if (!repeated.isEmpty())
274274
throw new MethodRedeclaredError(repeated.get(0).name.location(), "Method " + repeated.get(0).name.lexeme() + " already exists in class");

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ts/IOClassEntry.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.List;
1010

1111
public class IOClassEntry extends ClassEntry {
12+
private final OutArrayMethodEntry outArrayMethod;
13+
1214
public IOClassEntry(TinyRustSymbolTable root) {
1315
super(root, new TinyRustLexerToken(TinyRustTokenType.CLASS_ID, "IO"), true, false);
1416
// static fn out string(Str: s)->void: imprime el argumento.
@@ -25,13 +27,23 @@ public IOClassEntry(TinyRustSymbolTable root) {
2527
createMethod(root, "out_i32", TypeEntry.VOID, "out_i32", TypeEntry.I32);
2628
createMethod(root, "out_bool", TypeEntry.VOID, "out_bool", TypeEntry.BOOL);
2729
createMethod(root, "out_char", TypeEntry.VOID, "out_char", TypeEntry.CHAR);
28-
// createMethod(root, "out_array", TypeEntry.VOID, "out_array", TypeEntry.ARRAY);
30+
outArrayMethod = new OutArrayMethodEntry(root, this);
31+
addMethod(outArrayMethod);
2932
createMethod(root, "in_str", TypeEntry.STR);
3033
createMethod(root, "in_i32", TypeEntry.I32);
3134
createMethod(root, "in_bool", TypeEntry.BOOL);
3235
createMethod(root, "in_char", TypeEntry.CHAR);
3336
}
3437

38+
@Override
39+
protected void consolidateMethods() {
40+
checkDuplicateMethodNames();
41+
checkOverwriting();
42+
methods.stream().filter(it->!it.equals(outArrayMethod)).forEach(MethodEntry::consolidate);
43+
}
44+
45+
46+
3547
private void createMethod(TinyRustSymbolTable root, String name, TypeEntry ret) {
3648
MethodEntry m = new MethodEntry(
3749
root,

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ts/MethodEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static ko.carbonel.json.SerializeClass.AST;
2121
import static ko.carbonel.json.SerializeClass.TS;
2222

23-
public final class MethodEntry extends AttributeEntry implements Scope {
23+
public class MethodEntry extends AttributeEntry implements Scope {
2424
public static final String CONSTRUCTOR = "create";
2525
private final TinyRustSymbolTable root;
2626
private Scope parentScope;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ko.carbonel.compiler.intermediate.tinyRust.ts;
2+
3+
import ko.carbonel.compiler.exception.semantic.IncompatibleTypesError;
4+
import ko.carbonel.compiler.exception.semantic.WrongNumberOfArgumentsError;
5+
import ko.carbonel.compiler.intermediate.tinyRust.TinyRustSymbolTable;
6+
import ko.carbonel.compiler.intermediate.tinyRust.ast.expression.ExpressionEntry;
7+
import ko.carbonel.compiler.intermediate.tinyRust.ast.statement.BlockStatementEntry;
8+
import ko.carbonel.compiler.stream.lexer.tinyRust.model.TinyRustLexerToken;
9+
import ko.carbonel.compiler.stream.reader.FileLocation;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* Special method for outputting an array of any type to the console.
16+
*/
17+
public class OutArrayMethodEntry extends MethodEntry {
18+
public OutArrayMethodEntry(TinyRustSymbolTable root, Scope parentScope) {
19+
super(root, parentScope);
20+
this.parameters = List.of(new VariableEntry(root, TinyRustLexerToken.attr("arr"), TypeEntry.ARRAY, false));
21+
this.returnType = TypeEntry.VOID;
22+
this.name = TinyRustLexerToken.attr("out_array");
23+
this.block = new BlockStatementEntry();
24+
this.block.statements = new ArrayList<>();
25+
}
26+
27+
/**
28+
* Verifies that the arguments are valid for this method. Custom array only check
29+
* @param arguments The incoming arguments
30+
* @param scope The scope to use for type checking
31+
* @param location The location of the method call
32+
*/
33+
@Override
34+
public void verifyArguments(List<ExpressionEntry> arguments, Scope scope, FileLocation location) {
35+
if (arguments.size() != 1) {
36+
throw new WrongNumberOfArgumentsError(location, "Expected 1 argument, got " + arguments.size());
37+
}
38+
TypeEntry arg0Type = arguments.get(0).getType(scope, false);
39+
if (!arg0Type.isArray) {
40+
throw new IncompatibleTypesError(location, "Expected array argument, got " + arg0Type);
41+
}
42+
}
43+
}

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ts/TypeEntry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class TypeEntry {
1919
public static final TypeEntry VOID = of(new TinyRustLexerToken(TinyRustTokenType.VOID, "void"));
2020
public static final TypeEntry BOOL = of(new TinyRustLexerToken(TinyRustTokenType.BOOL, "Bool"));
2121
public static final TypeEntry CHAR = of(new TinyRustLexerToken(TinyRustTokenType.CHR, "Char"));
22+
public static final TypeEntry ARRAY = new TypeEntry(new TinyRustLexerToken(TinyRustTokenType.ARRAY, "Array"), true);
2223
private ClassEntry classLink;
2324
@SerializeClass(TS)
2425
public final TinyRustLexerToken type;

src/test/resources/ast/20.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*OK
2+
*/
3+
fn main(){
4+
(IO.out_array(new I32[5+2]));
5+
(IO.out_array(new Str[1]));
6+
(IO.out_array(new Char[0]));
7+
(IO.out_array(new Bool[0]));
8+
}

src/test/resources/ast/20.rs.full

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*OK
2+
*/
3+
fn main(){
4+
(IO.out_array(new I32[(5+2)]));
5+
(IO.out_array(new Str[1]));
6+
(IO.out_array(new Char[0]));
7+
(IO.out_array(new Bool[0]));
8+
}

src/test/resources/ast/21.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*ERROR
2+
IncompatibleTypesError
3+
*/
4+
fn main(){
5+
(IO.out_array(5));
6+
}

src/test/resources/ast/22.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*ERROR
2+
IncompatibleTypesError
3+
*/
4+
fn main(){
5+
(IO.out_array("Hi there"));
6+
}

0 commit comments

Comments
 (0)