11import { TOKEN } from "../../lexer/token" ;
22import { Parser } from "../parser" ;
33import { Expr } from "./expr" ;
4- import { Identifier } from "./identifier" ;
54import { PRECEDENCE } from "./precedence" ;
65
76
87export class Hash implements Expr {
9- map : Map < string , Expr > ;
8+ map : Map < Expr , Expr > ;
109 isApplicable ( token : TOKEN ) : boolean {
1110 return token === TOKEN . LBRACE ;
1211 }
1312 parse ( p : Parser ) : Expr {
14- const mp = new Map < string , Expr > ( ) ;
13+ const mp = new Map < Expr , Expr > ( ) ;
1514 p . readExpectedToken ( TOKEN . LBRACE ) ;
1615 if ( p . nextTokenIs ( TOKEN . RBRACE ) ) {
1716 return Hash . empty ( ) ;
@@ -25,24 +24,24 @@ export class Hash implements Expr {
2524 return Hash . create ( mp ) ;
2625 }
2726
28- parseEntry ( p : Parser , mp : Map < string , Expr > ) : void {
29- const name = new Identifier ( ) . parse ( p ) . toString ( ) ;
30- p . readExpectedToken ( TOKEN . ASSIGN ) ;
27+ parseEntry ( p : Parser , mp : Map < Expr , Expr > ) : void {
28+ const name = p . parseExpr ( PRECEDENCE . LOWEST ) ;
29+ p . readExpectedToken ( TOKEN . COLON ) ;
3130 const val = p . parseExpr ( PRECEDENCE . LOWEST ) ;
3231 if ( mp . has ( name ) ) {
3332 throw Error ( `${ name } is already declared in the object` ) ;
3433 }
3534 mp . set ( name , val ) ;
3635 }
3736
38- static create ( mp : Map < string , Expr > ) {
37+ static create ( mp : Map < Expr , Expr > ) {
3938 const h = new Hash ( ) ;
4039 h . map = mp ;
4140 return h ;
4241 }
4342
4443 static empty ( ) : Expr {
45- const mp = new Map < string , Expr > ( ) ;
44+ const mp = new Map < Expr , Expr > ( ) ;
4645 const h = new Hash ( ) ;
4746 h . map = mp ;
4847 return h ;
@@ -62,9 +61,9 @@ export class Hash implements Expr {
6261 }
6362
6463 eval ( ) {
65- const mp : Map < string , any > = new Map < string , any > ( ) ;
64+ const mp : Map < any , any > = new Map < any , any > ( ) ;
6665 for ( const [ k , v ] of this . map . entries ( ) ) {
67- mp . set ( k , v . eval ( ) ) ;
66+ mp . set ( k . eval ( ) , v . eval ( ) ) ;
6867 }
6968 return mp ;
7069 }
0 commit comments