@@ -159,6 +159,9 @@ private Statement statement() {
159159 if (match (TokenType .MATCH )) {
160160 return match ();
161161 }
162+ if (match (TokenType .CLASS )) {
163+ return classDeclaration ();
164+ }
162165 if (lookMatch (0 , TokenType .WORD ) && lookMatch (1 , TokenType .LPAREN )) {
163166 return new ExprStatement (functionChain (qualifiedName ()));
164167 }
@@ -437,6 +440,30 @@ private MatchExpression match() {
437440
438441 return new MatchExpression (expression , patterns );
439442 }
443+
444+ private Statement classDeclaration () {
445+ // class Name {
446+ // x = 123
447+ // str = ""
448+ // def method() = str
449+ // }
450+ final String name = consume (TokenType .WORD ).getText ();
451+ final ClassDeclarationStatement classDeclaration = new ClassDeclarationStatement (name );
452+ consume (TokenType .LBRACE );
453+ do {
454+ if (match (TokenType .DEF )) {
455+ classDeclaration .addMethod (functionDefine ());
456+ } else {
457+ final AssignmentExpression fieldDeclaration = assignmentStrict ();
458+ if (fieldDeclaration != null ) {
459+ classDeclaration .addField (fieldDeclaration );
460+ } else {
461+ throw new ParseException ("Class can contain only assignments and function declarations" );
462+ }
463+ }
464+ } while (!match (TokenType .RBRACE ));
465+ return classDeclaration ;
466+ }
440467
441468 private Expression expression () {
442469 return assignment ();
@@ -450,7 +477,7 @@ private Expression assignment() {
450477 return ternary ();
451478 }
452479
453- private Expression assignmentStrict () {
480+ private AssignmentExpression assignmentStrict () {
454481 // x[0].prop += ...
455482 final int position = pos ;
456483 final Expression targetExpr = qualifiedName ();
@@ -667,30 +694,45 @@ private Expression additive() {
667694 }
668695
669696 private Expression multiplicative () {
670- Expression result = unary ();
697+ Expression result = objectCreation ();
671698
672699 while (true ) {
673700 if (match (TokenType .STAR )) {
674- result = new BinaryExpression (BinaryExpression .Operator .MULTIPLY , result , unary ());
701+ result = new BinaryExpression (BinaryExpression .Operator .MULTIPLY , result , expression ());
675702 continue ;
676703 }
677704 if (match (TokenType .SLASH )) {
678- result = new BinaryExpression (BinaryExpression .Operator .DIVIDE , result , unary ());
705+ result = new BinaryExpression (BinaryExpression .Operator .DIVIDE , result , expression ());
679706 continue ;
680707 }
681708 if (match (TokenType .PERCENT )) {
682- result = new BinaryExpression (BinaryExpression .Operator .REMAINDER , result , unary ());
709+ result = new BinaryExpression (BinaryExpression .Operator .REMAINDER , result , expression ());
683710 continue ;
684711 }
685712 if (match (TokenType .STARSTAR )) {
686- result = new BinaryExpression (BinaryExpression .Operator .POWER , result , unary ());
713+ result = new BinaryExpression (BinaryExpression .Operator .POWER , result , expression ());
687714 continue ;
688715 }
689716 break ;
690717 }
691718
692719 return result ;
693720 }
721+
722+ private Expression objectCreation () {
723+ if (match (TokenType .NEW )) {
724+ final String className = consume (TokenType .WORD ).getText ();
725+ final List <Expression > args = new ArrayList <>();
726+ consume (TokenType .LPAREN );
727+ while (!match (TokenType .RPAREN )) {
728+ args .add (expression ());
729+ match (TokenType .COMMA );
730+ }
731+ return new ObjectCreationExpression (className , args );
732+ }
733+
734+ return unary ();
735+ }
694736
695737 private Expression unary () {
696738 if (match (TokenType .PLUSPLUS )) {
0 commit comments