Skip to content

Commit

Permalink
TypeDeclare & TypeAnnotation Interpreter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Jan 27, 2019
1 parent b85e94a commit 3efb3eb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
40 changes: 33 additions & 7 deletions server/parser/zsInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,42 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
};
}

protected TypeDeclare(ctx: NodeContext): ASTNode {
return {
type: 'type-declare',
};
protected TypeDeclare(ctx: NodeContext) {
return this.visit(ctx.TypeAnnotation);
}

protected TypeAnnotation(ctx: NodeContext): ASTNode {
return {
type: 'type',
};
// Type from ANY - STRING
if (Object.keys(ctx).length === 1) {
return {
type: Object.keys(ctx)[0],
};
}

// Imported type
if (ctx.IDENTIFIER) {
return {
type: 'IMPORT',
item: ctx.IDENTIFIER.map((identifier: IToken) => identifier.image),
};
}

// Function type
if (ctx.FUNCTION) {
return {
type: 'FUNCTION',
item: ctx.ParameterType.map((type: any) => this.visit(type)),
return: this.visit(ctx.FunctionType),
};
}

// Array type
if (ctx.ArrayType) {
return {
type: 'ARRAY',
item: this.visit(ctx.ArrayType),
};
}
}
}

Expand Down
83 changes: 43 additions & 40 deletions server/parser/zsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,49 +627,52 @@ export class ZenScriptParser extends Parser {
});

protected TypeAnnotation = this.RULE('TypeAnnotation', () => {
this.OR([
{ ALT: () => this.CONSUME(ANY) },
{ ALT: () => this.CONSUME(VOID) },
{ ALT: () => this.CONSUME(BOOL) },
{ ALT: () => this.CONSUME(BYTE) },
{ ALT: () => this.CONSUME(SHORT) },
{ ALT: () => this.CONSUME(INT) },
{ ALT: () => this.CONSUME(LONG) },
{ ALT: () => this.CONSUME(FLOAT) },
{ ALT: () => this.CONSUME(DOUBLE) },
{ ALT: () => this.CONSUME(STRING) },
{
ALT: () => {
this.AT_LEAST_ONE_SEP({
SEP: DOT,
DEF: () => {
this.CONSUME(IDENTIFIER);
},
});
this.OR({
DEF: [
{ ALT: () => this.CONSUME(ANY) },
{ ALT: () => this.CONSUME(VOID) },
{ ALT: () => this.CONSUME(BOOL) },
{ ALT: () => this.CONSUME(BYTE) },
{ ALT: () => this.CONSUME(SHORT) },
{ ALT: () => this.CONSUME(INT) },
{ ALT: () => this.CONSUME(LONG) },
{ ALT: () => this.CONSUME(FLOAT) },
{ ALT: () => this.CONSUME(DOUBLE) },
{ ALT: () => this.CONSUME(STRING) },
{
ALT: () => {
this.AT_LEAST_ONE_SEP({
SEP: DOT,
DEF: () => {
this.CONSUME(IDENTIFIER);
},
});
},
},
},
{
ALT: () => {
this.CONSUME(FUNCTION);
this.CONSUME(BR_OPEN);
this.MANY_SEP({
SEP: COMMA,
DEF: () => {
this.SUBRULE(this.TypeAnnotation);
},
});
this.CONSUME(BR_CLOSE);
this.SUBRULE2(this.TypeAnnotation);
{
ALT: () => {
this.CONSUME(FUNCTION);
this.CONSUME(BR_OPEN);
this.MANY_SEP({
SEP: COMMA,
DEF: () => {
this.SUBRULE(this.TypeAnnotation, { LABEL: 'ParameterType' });
},
});
this.CONSUME(BR_CLOSE);
this.SUBRULE2(this.TypeAnnotation, { LABEL: 'FunctionType' });
},
},
},
{
ALT: () => {
this.CONSUME(SQBR_OPEN);
this.SUBRULE3(this.TypeAnnotation);
this.CONSUME(SQBR_CLOSE);
{
ALT: () => {
this.CONSUME(SQBR_OPEN);
this.SUBRULE3(this.TypeAnnotation, { LABEL: 'ArrayType' });
this.CONSUME(SQBR_CLOSE);
},
},
},
]);
],
ERR_MSG: 'Must be a type.',
});
this.MANY(() => {
this.CONSUME2(SQBR_OPEN);
this.CONSUME2(SQBR_CLOSE);
Expand Down

0 comments on commit 3efb3eb

Please sign in to comment.