@@ -336,6 +336,9 @@ export function createExecutable(tree) {
336336 } ) ;
337337 return new Praxly_array_literal ( args , tree ) ;
338338
339+ case NODETYPES . ARRAY_CREATE :
340+ return new Praxly_array_create ( tree . varType , tree . name , tree . elemType , createExecutable ( tree . arrayLength ) , tree ) ;
341+
339342 case NODETYPES . ARRAY_REFERENCE :
340343 return new Praxly_array_reference ( tree . name , createExecutable ( tree . index ) , tree ) ;
341344
@@ -353,7 +356,6 @@ export function createExecutable(tree) {
353356class Praxly_single_line_comment {
354357
355358 constructor ( value , node ) {
356- this . jsonType = 'Praxly_single_line_comment' ;
357359 this . json = node ;
358360 this . value = value ;
359361 }
@@ -365,7 +367,6 @@ class Praxly_single_line_comment {
365367class Praxly_comment {
366368
367369 constructor ( value , node ) {
368- this . jsonType = 'Praxly_comment' ;
369370 this . json = node ;
370371 this . value = value ;
371372 }
@@ -377,7 +378,6 @@ class Praxly_comment {
377378class Praxly_int {
378379
379380 constructor ( value , node ) {
380- this . jsonType = 'Praxly_int' ;
381381 this . json = node ;
382382 this . value = Math . floor ( value ) ;
383383 this . realType = TYPES . INT ;
@@ -391,7 +391,6 @@ class Praxly_int {
391391class Praxly_short {
392392
393393 constructor ( value , node ) {
394- this . jsonType = 'Praxly_int' ;
395394 this . json = node ;
396395 this . value = Math . floor ( value ) ;
397396 this . realType = TYPES . SHORT ;
@@ -405,7 +404,6 @@ class Praxly_short {
405404class Praxly_double {
406405
407406 constructor ( value , node ) {
408- this . jsonType = 'Praxly_double' ;
409407 this . json = node ;
410408 this . value = parseFloat ( value ) ;
411409 this . realType = TYPES . DOUBLE ;
@@ -419,7 +417,6 @@ class Praxly_double {
419417class Praxly_float {
420418
421419 constructor ( value , node ) {
422- this . jsonType = 'Praxly_double' ;
423420 this . json = node ;
424421 this . value = parseFloat ( value ) ;
425422 this . realType = TYPES . FLOAT ;
@@ -434,7 +431,6 @@ class Praxly_boolean {
434431
435432 constructor ( value , node ) {
436433 this . json = node ;
437- this . jsonType = 'Praxly_boolean' ;
438434 this . value = value ;
439435 this . realType = TYPES . BOOLEAN ;
440436 }
@@ -449,7 +445,6 @@ class Praxly_char {
449445 constructor ( value , node ) {
450446 this . value = value ;
451447 this . json = node ;
452- this . jsonType = 'Praxly_String' ;
453448 this . realType = TYPES . CHAR ;
454449 }
455450
@@ -461,7 +456,6 @@ class Praxly_char {
461456class Praxly_String {
462457
463458 constructor ( value , node ) {
464- this . jsonType = 'Praxly_String' ;
465459 this . json = node ;
466460 this . value = value ;
467461 this . realType = TYPES . STRING ;
@@ -475,7 +469,6 @@ class Praxly_String {
475469class Praxly_null {
476470
477471 constructor ( value , node ) {
478- this . jsonType = 'Praxly_null' ;
479472 this . json = node ;
480473 this . value = value ;
481474 this . realType = TYPES . NULL ;
@@ -488,35 +481,91 @@ class Praxly_null {
488481
489482class Praxly_array_literal {
490483
491- constructor ( elements , node ) {
484+ constructor ( elements , node , elemType ) {
492485 this . elements = elements ;
493486 this . json = node ;
494- this . jsonType = 'Praxly_array' ;
495-
496- // set array type to "largest type" of element
497- let types = [ "boolean" , "char" , "short" , "int" , "float" , "double" , "String" ] ;
498- let max_type = - 1 ;
499- for ( let i = 0 ; i < elements . length ; i ++ ) {
500- let cur_type = types . indexOf ( elements [ i ] . realType ) ;
501- if ( cur_type > max_type ) {
502- max_type = cur_type ;
487+
488+ if ( elemType ) {
489+ this . realType = elemType + "[]" ;
490+ } else {
491+ // set array type to "largest type" of element
492+ let types = [ "boolean" , "char" , "short" , "int" , "float" , "double" , "String" ] ;
493+ let max_type = - 1 ;
494+ for ( let i = 0 ; i < elements . length ; i ++ ) {
495+ let cur_type = types . indexOf ( elements [ i ] . realType ) ;
496+ if ( cur_type > max_type ) {
497+ max_type = cur_type ;
498+ }
503499 }
500+ this . realType = types [ max_type ] + "[]" ;
504501 }
505- this . realType = types [ max_type ] + "[]" ;
506502 }
507503
508504 async evaluate ( environment ) {
509505 return this ;
510506 }
511507}
512508
509+ class Praxly_array_create {
510+
511+ constructor ( varType , varName , elemType , arrayLength , node ) {
512+ this . varType = varType ;
513+ this . varName = varName ;
514+ this . elemType = elemType ;
515+ this . arrayLength = arrayLength ;
516+ this . json = node ;
517+ }
518+
519+ async evaluate ( environment ) {
520+ // type checking
521+ if ( ! can_assign ( this . varType , this . elemType , this . json . line ) ) {
522+ throw new PraxlyError ( `Array element did not match declared type ` +
523+ `(Expected: ${ this . varType } , Actual: ${ this . elemType } )` , this . json . line ) ;
524+ }
525+ let length = await this . arrayLength . evaluate ( environment ) ;
526+ if ( length . realType != NODETYPES . INT ) {
527+ throw new PraxlyError ( `Array length must be an integer ` +
528+ `(Actual: ${ length . realType } )` , this . json . line ) ;
529+ }
530+ length = length . value ;
531+ if ( length < 0 ) {
532+ throw new PraxlyError ( `Array length must be nonnegative ` +
533+ `(Actual: ${ length } )` , this . json . line ) ;
534+ }
535+ // default value for new array of n elements
536+ let value ;
537+ switch ( this . elemType ) {
538+ case TYPES . BOOLEAN :
539+ value = false ;
540+ break ;
541+ case TYPES . CHAR :
542+ value = "0" ; // null character not in language
543+ break ;
544+ case TYPES . STRING :
545+ value = "" ; // null reference not implemented
546+ break ;
547+ default :
548+ value = 0 ;
549+ break ;
550+ }
551+ // construct the array of n default values
552+ let elements = [ ] ;
553+ for ( let i = 0 ; i < length ; i ++ ) {
554+ elements . push ( litNode_new ( this . elemType , value , this . json ) ) ;
555+ }
556+ // array is being declared and initialized
557+ let array = new Praxly_array_literal ( elements , this . json , this . elemType ) ;
558+ environment . variableList [ this . varName ] = array ;
559+ }
560+ }
561+
513562export function valueToString ( child , quotes , line ) {
514563 if ( child === "Exit_Success" ) {
515564 throw new PraxlyError ( "no value returned from void procedure" , line ) ;
516565 }
517566 var result ;
518- if ( child . jsonType === 'Praxly_array' ) {
519- // always show quote marks for arrays
567+ if ( child instanceof Praxly_array_literal ) {
568+ // always show curly braces for arrays
520569 let values = child . elements . map ( ( element ) => valueToString ( element , true , line ) ) ;
521570 result = '{' + values . join ( ", " ) + '}' ;
522571 } else {
0 commit comments