onelang
Version:
OneLang transpiler framework core
171 lines (161 loc) • 4.96 kB
YAML
name: javascript
extension: js
casing:
class: pascal_case
method: camel_case
field: camel_case
property: camel_case
variable: camel_case
enum: pascal_case
enumMember: upper_case
primitiveTypes:
void: void
any: object
templates:
testGenerator: |-
try {
new {{class}}().{{method}}();
} catch(e) {
console.log(`Exception: ${e.message}`);
}
main: |-
{{for inc in includes|sep=\n}}
const {{inc.name}} = require('{{inc.source}}');
{{/for}}
{{for enum in enums|sep=\n\n}}
const {{enum.name}} = Object.freeze({
{{for item in enum.values}}
{{item.name}}: "{{item.origName}}",
{{/for}}
});
{{/for}}
{{for class in classes|sep=\n\n}}
class {{class.name}}
{{if class.baseClass|inline}}
extends {{class.baseClass}}
{{/if}}
{
{{if class.needsConstructor}}
constructor({{if class.constructor}}{{genArgs(class.constructor)}}{{/if}}) {
{{for field in class.fields|sep=\n}}
{{if field.static == false && field.initializer}}
this.{{field.name}} = {{gen(field.initializer)}};
{{/if}}
{{/for}}
{{if class.constructor}}
{{genBody(class.constructor.body)}}
{{/if}}
}
{{/if}}
{{for method in class.methods|sep="\n \n"}}
{{if method.static}}static {{/if}}{{method.name}}({{genArgs(method)}}) {
{{genBody(method.body)}}
}
{{/for}}
}
{{for field in class.fields|sep=\n}}
{{if field.static && field.initializer}}
{{class.name}}.{{field.name}} = {{gen(field.initializer)}};
{{/if}}
{{/for}}
{{endClass(class)}}
{{/for}}
{{genBody(mainBlock)}}
genBody:
args:
- name: body
template: |-
{{for statement in body.statements|sep=\n}}
{{statement.leadingTrivia}}{{gen(statement)}}
{{/for}}
genArgs:
args:
- name: method
template: |-
{{for param in method.parameters|sep=", "}}
{{param.name}}
{{/for}}
genParams:
args:
- name: params
template: |-
{{for param in params|sep=", " inline}}
{{gen(param)}}
{{/for}}
genVar:
args:
- name: itemVar
template: "{{if itemVar.isMutable}}let{{else}}const{{/if}} {{itemVar.outName}} = {{gen(itemVar.initializer)}}"
expressions:
call: |-
{{gen(expr.method)}}(
{{for arg in expr.arguments|sep=", " inline}}
{{gen(arg)}}
{{/for}}
)
propertyAccess: "{{gen(expr.object)}}.{{gen(expr.propertyName)}}"
identifier: "{{expr.text}}"
stringLiteral: '"{{expr.escapedText}}"'
characterLiteral: '"{{expr.escapedText}}"'
nullLiteral: "null"
return: "return{{if expr.expression}} {{gen(expr.expression)}}{{/if}};"
binary: "{{gen(expr.left)}} {{expr.operator}} {{gen(expr.right)}}"
postfix: "{{gen(expr.operand)}}{{expr.operator}}"
prefix: "{{expr.operator}}{{gen(expr.operand)}}"
throw: "throw {{gen(expr.expression)}};"
parenthesized: "({{gen(expr.expression)}})"
numericLiteral: "{{expr.value}}"
variableDeclaration: "{{genVar(expr)}};"
new: "new {{gen(expr.cls)}}({{genParams(expr.arguments)}})"
classReference: "{{clsName(expr.classRef)}}"
enumReference: "{{expr.enumRef.outName}}"
enumMemberReference: "{{expr.enumRef.outName}}.{{expr.enumMemberRef.outName}}"
expressionStatement: "{{gen(expr.expression)}};"
instanceMethod: "{{gen(expr.thisExpr)}}.{{expr.methodRef.outName}}"
staticMethod: "{{clsName(expr.methodRef.classRef)}}.{{expr.methodRef.outName}}"
localVar: "{{expr.varRef.outName}}"
methodArgument: "{{expr.varRef.outName}}"
instanceField: "{{gen(expr.thisExpr)}}.{{expr.varRef.outName}}"
staticField: "{{gen(expr.thisExpr)}}.{{expr.varRef.outName}}"
falseLiteral: "false"
trueLiteral: "true"
elementAccess: "{{gen(expr.object)}}[{{gen(expr.elementExpr)}}]"
thisReference: this
conditional: "{{gen(expr.condition)}} ? {{gen(expr.whenTrue)}} : {{gen(expr.whenFalse)}}"
break: break
templateString: |-
`
{{for part in expr.parts|inline}}
{{if part.literal|inline}}
{{part.text}}
{{else}}
${{{gen(part.expr)}}}
{{/if}}
{{/for}}
`
foreach: |-
for (const {{expr.itemVariable.outName}} of {{gen(expr.items)}}) {
{{genBody(expr.body)}}
}
for: |-
for ({{genVar(expr.itemVariable)}}; {{gen(expr.condition)}}; {{gen(expr.incrementor)}}) {
{{genBody(expr.body)}}
}
while: |-
while ({{gen(expr.condition)}}) {
{{genBody(expr.body)}}
}
if: |-
if ({{gen(expr.condition)}}) {
{{genBody(expr.then)}}
}
{{if expr.else|inline}}
{{" "}}
{{if isIfBlock(expr.else)|inline}}
else {{genBody(expr.else)}}
{{else}}
else {
{{genBody(expr.else)}}
}
{{/if}}
{{/if}}