Skip to content

Commit f97470d

Browse files
committed
cast object
1 parent 2ad4d34 commit f97470d

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

libCompiler/src/main/java/com/duy/pascal/backend/ast/function_declaretion/builtin/CastObjectFunction.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
package com.duy.pascal.backend.ast.function_declaretion.builtin;
1818

1919

20-
import com.duy.pascal.backend.parse_exception.ParsingException;
21-
import com.duy.pascal.backend.linenumber.LineInfo;
22-
import com.duy.pascal.backend.pascaltypes.ArgumentType;
23-
import com.duy.pascal.backend.pascaltypes.DeclaredType;
24-
import com.duy.pascal.backend.pascaltypes.JavaClassBasedType;
25-
import com.duy.pascal.backend.pascaltypes.RuntimeType;
20+
import com.duy.pascal.backend.ast.codeunit.RuntimeExecutableCodeUnit;
2621
import com.duy.pascal.backend.ast.expressioncontext.CompileTimeContext;
2722
import com.duy.pascal.backend.ast.expressioncontext.ExpressionContext;
2823
import com.duy.pascal.backend.ast.instructions.Executable;
24+
import com.duy.pascal.backend.ast.runtime_value.VariableContext;
25+
import com.duy.pascal.backend.ast.runtime_value.references.PascalReference;
2926
import com.duy.pascal.backend.ast.runtime_value.value.FunctionCall;
3027
import com.duy.pascal.backend.ast.runtime_value.value.RuntimeValue;
31-
import com.duy.pascal.backend.ast.runtime_value.references.PascalReference;
32-
import com.duy.pascal.backend.ast.runtime_value.VariableContext;
33-
import com.duy.pascal.backend.ast.codeunit.RuntimeExecutableCodeUnit;
28+
import com.duy.pascal.backend.linenumber.LineInfo;
29+
import com.duy.pascal.backend.parse_exception.ParsingException;
30+
import com.duy.pascal.backend.pascaltypes.ArgumentType;
31+
import com.duy.pascal.backend.pascaltypes.DeclaredType;
32+
import com.duy.pascal.backend.pascaltypes.JavaClassBasedType;
33+
import com.duy.pascal.backend.pascaltypes.PointerType;
34+
import com.duy.pascal.backend.pascaltypes.RuntimeType;
3435
import com.duy.pascal.backend.runtime_exception.RuntimePascalException;
3536

3637
/**
@@ -43,16 +44,18 @@ public class CastObjectFunction implements IMethodDeclaration {
4344
new RuntimeType(new JavaClassBasedType(Object.class), false)};
4445

4546
@Override
46-
public String getName() {
47+
public String getName() {
4748
return "cast";
4849
}
4950

5051
@Override
5152
public FunctionCall generateCall(LineInfo line, RuntimeValue[] arguments,
52-
ExpressionContext f) throws ParsingException {
53+
ExpressionContext f) throws ParsingException {
5354
RuntimeValue pointer = arguments[0];
5455
RuntimeValue value = arguments[1];
55-
return new InstanceObjectCall(pointer, value, line);
56+
PointerType declType = (PointerType) pointer.getType(f).declType;
57+
Class<?> storageClass = declType.pointedToType.getStorageClass();
58+
return new InstanceObjectCall(pointer, value, storageClass, line);
5659
}
5760

5861
@Override
@@ -77,12 +80,14 @@ public String description() {
7780

7881
private class InstanceObjectCall extends FunctionCall {
7982
private RuntimeValue value;
83+
private Class<?> storageClass;
8084
private LineInfo line;
8185
private RuntimeValue pointer;
8286

83-
InstanceObjectCall(RuntimeValue pointer, RuntimeValue value, LineInfo line) {
87+
InstanceObjectCall(RuntimeValue pointer, RuntimeValue value, Class<?> storageClass, LineInfo line) {
8488
this.value = value;
8589
this.pointer = pointer;
90+
this.storageClass = storageClass;
8691
this.line = line;
8792
}
8893

@@ -105,13 +110,13 @@ public Object compileTimeValue(CompileTimeContext context) {
105110
@Override
106111
public RuntimeValue compileTimeExpressionFold(CompileTimeContext context)
107112
throws ParsingException {
108-
return new InstanceObjectCall(pointer, value, line);
113+
return new InstanceObjectCall(pointer, value, storageClass, line);
109114
}
110115

111116
@Override
112117
public Executable compileTimeConstantTransform(CompileTimeContext c)
113118
throws ParsingException {
114-
return new InstanceObjectCall(pointer, value, line);
119+
return new InstanceObjectCall(pointer, value, storageClass, line);
115120
}
116121

117122
@Override
@@ -129,13 +134,8 @@ public Object getValueImpl(VariableContext f, RuntimeExecutableCodeUnit<?> main)
129134
//indexOf value of arg 2
130135
Object value = this.value.getValue(f, main);
131136

132-
//indexOf value of variable
133-
Object o = pointer.get();
134-
//indexOf class of variable
135-
Class<?> aClass = o.getClass();
136-
137137
//cast object to type of variable
138-
Object casted = aClass.cast(value);
138+
Object casted = storageClass.cast(value);
139139

140140
//set value
141141
pointer.set(casted);

libCompiler/src/main/java/com/duy/pascal/backend/ast/runtime_value/operators/number/JavaBiOperatorEval.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
package com.duy.pascal.backend.ast.runtime_value.operators.number;
1818

19-
import com.duy.pascal.backend.parse_exception.ParsingException;
20-
import com.duy.pascal.backend.linenumber.LineInfo;
21-
import com.duy.pascal.backend.pascaltypes.BasicType;
22-
import com.duy.pascal.backend.pascaltypes.RuntimeType;
23-
import com.duy.pascal.backend.pascaltypes.OperatorTypes;
24-
import com.duy.pascal.backend.ast.runtime_value.operators.BinaryOperatorEval;
2519
import com.duy.pascal.backend.ast.expressioncontext.CompileTimeContext;
2620
import com.duy.pascal.backend.ast.expressioncontext.ExpressionContext;
21+
import com.duy.pascal.backend.ast.runtime_value.operators.BinaryOperatorEval;
2722
import com.duy.pascal.backend.ast.runtime_value.value.ConstantAccess;
2823
import com.duy.pascal.backend.ast.runtime_value.value.RuntimeValue;
24+
import com.duy.pascal.backend.linenumber.LineInfo;
25+
import com.duy.pascal.backend.parse_exception.ParsingException;
26+
import com.duy.pascal.backend.pascaltypes.BasicType;
27+
import com.duy.pascal.backend.pascaltypes.OperatorTypes;
28+
import com.duy.pascal.backend.pascaltypes.RuntimeType;
2929
import com.duy.pascal.backend.runtime_exception.PascalArithmeticException;
3030
import com.duy.pascal.backend.runtime_exception.internal.InternalInterpreterException;
3131

@@ -51,8 +51,14 @@ public Object operate(Object value1, Object value2)
5151
throws PascalArithmeticException, InternalInterpreterException {
5252
switch (operator_type) {
5353
case EQUALS:
54+
if (value1 == null || value2 == null) {
55+
return value1 == value2;
56+
}
5457
return value1.equals(value2);
5558
case NOTEQUAL:
59+
if (value1 == null || value2 == null) {
60+
return !(value1 == value2);
61+
}
5662
return !value1.equals(value2);
5763
default:
5864
throw new InternalInterpreterException(line);

libCompiler/src/test/java/com/duy/pascal/test_interpreter/ForStatementTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,32 @@
1818

1919
import com.duy.pascal.frontend.DLog;
2020

21-
import junit.framework.TestCase;
22-
2321
import static com.duy.pascal.Compiler.runProgram;
2422

2523
/**
2624
* Created by Duy on 29-May-17.
2725
*/
2826

29-
public class ForStatementTest extends TestCase {
27+
public class ForStatementTest extends BaseTestCase {
28+
@Override
29+
public String getDirTest() {
30+
return "C:\\github\\pascalnide\\test_pascal\\test_statement\\for_statement";
31+
}
32+
3033
@Override
3134
protected void setUp() throws Exception {
3235
super.setUp();
3336
DLog.ANDROID = false;
3437
}
3538

39+
public void test1() {
40+
run("test1.pas");
41+
}
42+
43+
public void test2() {
44+
run("test2.pas");
45+
}
46+
3647
public void testForInEnum() {
3748
try {
3849
runProgram("C:\\github\\pascalnide\\test_pascal\\test_statement\\test_for_in_enum.pas");
@@ -64,4 +75,5 @@ public void testForInArray() {
6475
}
6576

6677

78+
6779
}

test_pascal/test_location/test_get_long_lat.pas

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ procedure printData(data: map);
3939
if (data.get(name) <> null) then //if the data of provider is exist
4040
begin
4141
cast(info, data.get(name));
42-
4342
//print long,lat to console
44-
writeln('getLatitude = ', info.getLatitude());
45-
writeln('getLongitude = ', info.getLongitude());
43+
writeln('Latitude = ', info.getLatitude());
44+
writeln('Longitude = ', info.getLongitude());
4645
end;
4746
end;
48-
writeln;
47+
writeln('...');
4948
end;
5049

5150

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
2
3+
3
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var
2+
i: integer ;
3+
begin
4+
for i := 1 to 3 do writeln(i):
5+
end.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
4
2+
5
3+
6
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var
2+
i, j: integer;
3+
begin
4+
readln(j);
5+
for i := j + 1 to j +3 do writeln(i);
6+
end.

0 commit comments

Comments
 (0)