@@ -58,6 +58,7 @@ private void readLibrary()
58
58
readLibraryModule ( EV3BasicCompiler . Properties . Resources . Motor ) ;
59
59
readLibraryModule ( EV3BasicCompiler . Properties . Resources . Program ) ;
60
60
readLibraryModule ( EV3BasicCompiler . Properties . Resources . Sensor ) ;
61
+ readLibraryModule ( EV3BasicCompiler . Properties . Resources . Speaker ) ;
61
62
readLibraryModule ( EV3BasicCompiler . Properties . Resources . Text ) ;
62
63
readLibraryModule ( EV3BasicCompiler . Properties . Resources . Vector ) ;
63
64
}
@@ -75,8 +76,7 @@ private void readLibraryModule(String moduletext)
75
76
{
76
77
if ( currentfirstline == null )
77
78
{
78
- int idx = line . IndexOf ( "subcall" ) ;
79
- if ( idx == 0 )
79
+ if ( line . IndexOf ( "subcall" ) == 0 || line . IndexOf ( "inline" ) == 0 )
80
80
{
81
81
currentfirstline = line ;
82
82
body . Length = 0 ;
@@ -100,12 +100,13 @@ private void readLibraryModule(String moduletext)
100
100
body . AppendLine ( line ) ;
101
101
if ( line . IndexOf ( "}" ) == 0 )
102
102
{
103
- int idx1 = currentfirstline . IndexOf ( "subcall" ) + 7 ;
103
+ bool inline = currentfirstline . StartsWith ( "inline" ) ;
104
+ int idx1 = inline ? 6 : 7 ;
104
105
int idx2 = currentfirstline . IndexOf ( "//" , idx1 ) ;
105
106
String functionname = currentfirstline . Substring ( idx1 , idx2 - idx1 ) . Trim ( ) ;
106
107
String [ ] descriptorandreferences = currentfirstline . Substring ( idx2 + 2 ) . Trim ( ) . Split ( new char [ ] { ' ' , '\t ' } , StringSplitOptions . RemoveEmptyEntries ) ;
107
108
108
- LibraryEntry le = new LibraryEntry ( descriptorandreferences , body . ToString ( ) ) ;
109
+ LibraryEntry le = new LibraryEntry ( inline , descriptorandreferences , body . ToString ( ) ) ;
109
110
library [ functionname ] = le ;
110
111
111
112
currentfirstline = null ;
@@ -197,16 +198,6 @@ public void Compile(Stream source, Stream targetstream, List<String> errorlist)
197
198
{
198
199
target . WriteLine ( "DATAS S" + i + " 252" ) ;
199
200
}
200
- for ( int i = 0 ; maxreservedtemporaries . ContainsKey ( ExpressionType . NumberArray ) && i < maxreservedtemporaries [ ExpressionType . NumberArray ] ; i ++ )
201
- {
202
- target . WriteLine ( "ARRAY16 A" + i + " 2" ) ;
203
- initlist . WriteLine ( " CALL ARRAYCREATE_FLOAT A" + i ) ;
204
- }
205
- for ( int i = 0 ; maxreservedtemporaries . ContainsKey ( ExpressionType . TextArray ) && i < maxreservedtemporaries [ ExpressionType . TextArray ] ; i ++ )
206
- {
207
- target . WriteLine ( "ARRAY16 X" + i + " 2" ) ;
208
- initlist . WriteLine ( " CALL ARRAYCREATE_STRING X" + i ) ;
209
- }
210
201
target . WriteLine ( ) ;
211
202
212
203
target . WriteLine ( "vmthread MAIN" ) ;
@@ -223,7 +214,10 @@ public void Compile(Stream source, Stream targetstream, List<String> errorlist)
223
214
// { LibraryEntry le = (LibraryEntry) de.Value;
224
215
foreach ( LibraryEntry le in references )
225
216
{
226
- target . Write ( le . programCode ) ;
217
+ if ( ! le . inline )
218
+ {
219
+ target . Write ( le . programCode ) ;
220
+ }
227
221
}
228
222
target . Flush ( ) ;
229
223
}
@@ -246,13 +240,14 @@ public String reserveVariable(ExpressionType type)
246
240
return "F" + ( n - 1 ) ;
247
241
case ExpressionType . Text :
248
242
return "S" + ( n - 1 ) ;
249
- case ExpressionType . NumberArray :
250
- return "A" + ( n - 1 ) ;
251
- case ExpressionType . TextArray :
252
- return "X" + ( n - 1 ) ;
253
243
default :
254
- throw new Exception ( "Can not allocate temporary variable of type " + type ) ;
255
- }
244
+ s . ThrowParseError ( "Return value that is an array must be directly stored in a variable" ) ;
245
+ return "" ;
246
+ // case ExpressionType.NumberArray:
247
+ // return "A" + (n - 1);
248
+ // case ExpressionType.TextArray:
249
+ // return "X" + (n - 1);
250
+ }
256
251
}
257
252
public void releaseVariable ( ExpressionType type )
258
253
{
@@ -263,14 +258,14 @@ public void releaseVariable(ExpressionType type)
263
258
264
259
// --------------------------- TOP-DOWN PARSER -------------------------------
265
260
266
- private void memorize_reference ( String name )
261
+ public void memorize_reference ( String name )
267
262
{
268
263
LibraryEntry impl = ( LibraryEntry ) library [ name ] ;
269
264
if ( impl == null )
270
265
{
271
- throw new Exception ( "Reference to undefined function: " + name ) ;
266
+ s . ThrowParseError ( "Reference to undefined function: " + name ) ;
272
267
}
273
- if ( ! references . Contains ( impl ) )
268
+ else if ( ! references . Contains ( impl ) )
274
269
{
275
270
references . Add ( impl ) ;
276
271
for ( int i = 0 ; i < impl . references . Length ; i ++ )
@@ -331,7 +326,7 @@ private void compile_statement(TextWriter target)
331
326
}
332
327
else
333
328
{
334
- throw new Exception ( "Unknown PRAGMA: " + s . NextContent ) ;
329
+ s . ThrowParseError ( "Unknown PRAGMA: " + s . NextContent ) ;
335
330
}
336
331
s . GetSym ( ) ;
337
332
return ;
@@ -500,15 +495,13 @@ private void compile_for(TextWriter target)
500
495
}
501
496
incexpression = new CallExpression ( ExpressionType . Number , "ADDF" ,
502
497
new AtomicExpression ( ExpressionType . Number , varname ) , stepexpression ) ;
503
- memorize_reference ( "LE_STEP" ) ;
504
498
}
505
499
else
506
500
{
507
501
testexpression = new ComparisonExpression ( "CALL LE" , "JR_LTEQF" , "JR_GTF" ,
508
502
new AtomicExpression ( ExpressionType . Number , varname ) , stopexpression ) ;
509
503
incexpression = new CallExpression ( ExpressionType . Number , "ADDF" ,
510
504
new AtomicExpression ( ExpressionType . Number , varname ) , new NumberExpression ( 1.0 ) ) ;
511
- memorize_reference ( "LE" ) ;
512
505
}
513
506
parse_eol ( ) ;
514
507
@@ -635,7 +628,6 @@ private void compile_array_assignment(TextWriter target)
635
628
636
629
if ( e . type == ExpressionType . Text )
637
630
{
638
- memorize_reference ( "ARRAYSTORE_STRING" ) ;
639
631
Expression aex = new CallExpression ( ExpressionType . Void , "CALL ARRAYSTORE_STRING :0 :1 " + varname , eidx , e ) ;
640
632
aex . Generate ( this , target , null ) ;
641
633
}
@@ -660,7 +652,6 @@ private void compile_array_assignment(TextWriter target)
660
652
}
661
653
else
662
654
{
663
- memorize_reference ( "ARRAYSTORE_FLOAT" ) ;
664
655
Expression aex = new CallExpression ( ExpressionType . Void , "CALL ARRAYSTORE_FLOAT :0 :1 " + varname , eidx , e ) ;
665
656
aex . Generate ( this , target , null ) ;
666
657
}
@@ -695,7 +686,7 @@ private void compile_procedure_call(TextWriter target)
695
686
}
696
687
parse_special ( ")" ) ;
697
688
698
- Expression ex = new CallExpression ( ExpressionType . Void , "CALL " + functionname , list ) ;
689
+ Expression ex = new CallExpression ( ExpressionType . Void , libentry . inline ? libentry . programCode : ( "CALL " + functionname ) , list ) ;
699
690
if ( libentry . returnType == ExpressionType . Void )
700
691
{ ex . Generate ( this , target , null ) ;
701
692
}
@@ -705,8 +696,6 @@ private void compile_procedure_call(TextWriter target)
705
696
ex . Generate ( this , target , retvar ) ;
706
697
releaseVariable ( libentry . returnType ) ;
707
698
}
708
-
709
- memorize_reference ( functionname ) ;
710
699
}
711
700
712
701
@@ -759,7 +748,6 @@ private Expression parse_or_expression()
759
748
Expression right = parse_and_expression ( ) ;
760
749
if ( right . type != ExpressionType . Text ) s . ThrowParseError ( "need text on right side of OR" ) ;
761
750
total = new OrExpression ( total , right ) ;
762
- memorize_reference ( "OR" ) ;
763
751
}
764
752
else
765
753
{
@@ -782,7 +770,6 @@ private Expression parse_and_expression()
782
770
Expression right = parse_comparative_expression ( ) ;
783
771
if ( right . type != ExpressionType . Text ) s . ThrowParseError ( "need text on right side of AND" ) ;
784
772
total = new AndExpression ( total , right ) ;
785
- memorize_reference ( "AND" ) ;
786
773
}
787
774
else
788
775
{
@@ -850,7 +837,6 @@ private Expression parse_comparative_expression()
850
837
Expression right = parse_additive_expression ( ) ;
851
838
if ( right . type != ExpressionType . Number ) s . ThrowParseError ( "need number on right side of '<'" ) ;
852
839
total = new ComparisonExpression ( "CALL LT" , "JR_LTF" , "JR_GTEQF" , total , right ) ;
853
- memorize_reference ( "LT" ) ;
854
840
}
855
841
else if ( s . NextIsSPECIAL ( ">" ) )
856
842
{
@@ -859,7 +845,6 @@ private Expression parse_comparative_expression()
859
845
Expression right = parse_additive_expression ( ) ;
860
846
if ( right . type != ExpressionType . Number ) s . ThrowParseError ( "need number on right side of '>'" ) ;
861
847
total = new ComparisonExpression ( "CALL GT" , "JR_GTF" , "JR_LTEQF" , total , right ) ;
862
- memorize_reference ( "GT" ) ;
863
848
}
864
849
else if ( s . NextIsSPECIAL ( "<=" ) )
865
850
{
@@ -868,7 +853,6 @@ private Expression parse_comparative_expression()
868
853
Expression right = parse_additive_expression ( ) ;
869
854
if ( right . type != ExpressionType . Number ) s . ThrowParseError ( "need number on right side of '<='" ) ;
870
855
total = new ComparisonExpression ( "CALL LE" , "JR_LTEF" , "JR_GTF" , total , right ) ;
871
- memorize_reference ( "LE" ) ;
872
856
}
873
857
else if ( s . NextIsSPECIAL ( ">=" ) )
874
858
{
@@ -877,7 +861,6 @@ private Expression parse_comparative_expression()
877
861
Expression right = parse_additive_expression ( ) ;
878
862
if ( right . type != ExpressionType . Number ) s . ThrowParseError ( "need number on right side of '>='" ) ;
879
863
total = new ComparisonExpression ( "CALL GE" , "JR_GTEQF" , "JR_LTF" , total , right ) ;
880
- memorize_reference ( "GE" ) ;
881
864
}
882
865
else
883
866
{
@@ -912,15 +895,13 @@ private Expression parse_additive_expression()
912
895
s . ThrowParseError ( "Can not concat arrays" ) ;
913
896
}
914
897
total = new CallExpression ( ExpressionType . Text , "CALL TEXT.APPEND" , total , right ) ;
915
- memorize_reference ( "TEXT.APPEND" ) ;
916
898
}
917
899
else if ( total . type == ExpressionType . Number )
918
900
{
919
901
if ( right . type == ExpressionType . Text )
920
902
{
921
903
total = new CallExpression ( ExpressionType . Text , "STRINGS VALUE_FORMATTED :0 '%g' 99" , total ) ;
922
904
total = new CallExpression ( ExpressionType . Text , "CALL TEXT.APPEND" , total , right ) ;
923
- memorize_reference ( "TEXT.APPEND" ) ;
924
905
}
925
906
else if ( right . type == ExpressionType . Number )
926
907
{
@@ -1013,7 +994,6 @@ private Expression parse_multiplicative_expression()
1013
994
else
1014
995
{
1015
996
total = new CallExpression ( ExpressionType . Number , "CALL DIV" , total , right ) ;
1016
- memorize_reference ( "DIV" ) ;
1017
997
}
1018
998
}
1019
999
}
@@ -1065,7 +1045,7 @@ private Expression parse_atomic_expression()
1065
1045
String val = s . NextContent ;
1066
1046
if ( val . Length > 251 )
1067
1047
{
1068
- throw new Exception ( "Text is longer than 251 letters" ) ;
1048
+ s . ThrowParseError ( "Text is longer than 251 letters" ) ;
1069
1049
}
1070
1050
s . GetSym ( ) ;
1071
1051
return new AtomicExpression ( ExpressionType . Text , "'" + EscapeString ( val ) + "'" ) ;
@@ -1075,7 +1055,7 @@ private Expression parse_atomic_expression()
1075
1055
double val ;
1076
1056
if ( ! double . TryParse ( s . NextContent , NumberStyles . Float , CultureInfo . InvariantCulture , out val ) )
1077
1057
{
1078
- throw new Exception ( "Can not decode number: " + s . NextContent ) ;
1058
+ s . ThrowParseError ( "Can not decode number: " + s . NextContent ) ;
1079
1059
}
1080
1060
s . GetSym ( ) ;
1081
1061
return new NumberExpression ( val ) ;
@@ -1116,7 +1096,6 @@ private Expression parse_atomic_expression()
1116
1096
1117
1097
if ( atype == ExpressionType . Text )
1118
1098
{
1119
- memorize_reference ( "ARRAYGET_STRING" ) ;
1120
1099
return new CallExpression ( ExpressionType . Text , "CALL ARRAYGET_STRING :0 :1 " + varname , e ) ;
1121
1100
}
1122
1101
else
@@ -1127,7 +1106,6 @@ private Expression parse_atomic_expression()
1127
1106
}
1128
1107
else
1129
1108
{
1130
- memorize_reference ( "ARRAYGET_FLOAT" ) ;
1131
1109
return new CallExpression ( ExpressionType . Number , "CALL ARRAYGET_FLOAT :0 :1 " + varname , e ) ;
1132
1110
}
1133
1111
}
@@ -1198,8 +1176,7 @@ private Expression parse_function_call_or_property()
1198
1176
}
1199
1177
}
1200
1178
1201
- memorize_reference ( functionname ) ;
1202
- return new CallExpression ( libentry . returnType , "CALL " + functionname , list ) ;
1179
+ return new CallExpression ( libentry . returnType , libentry . inline ? libentry . programCode : ( "CALL " + functionname ) , list ) ;
1203
1180
}
1204
1181
1205
1182
private String parse_id ( )
0 commit comments