Skip to content

Commit

Permalink
[Prettify] Fix validity
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Jan 5, 2025
1 parent 8bbfe7a commit 786b7f1
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions source/server/prettify.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Prettify {
// TODO remove `:` here!
case Block(el):
// TODO depth!
`{\n` + el.map(e => this.stringify(e)).join('\n') + `\n}`
return `{\n\t` + el.map(e => this.stringify(e)).join('\n\t') + `\n}`

/// `if condition[0], ...condition[n] { then } [else { otherwise }]`
case If(condition, then, otherwise, ternary):
Expand Down Expand Up @@ -103,19 +103,23 @@ class Prettify {
/// `(vars) retType => expr`
// TODO parser `: retType` is really needed at all?
case Arrow(expr, vars, retType):
var head = '(' + vars.map(v => this.stringify(v).substr(4)).join(', ') + ')'

if let retType = retType {
return '(' + vars.map(v => this.stringify(v)).join(', ') + ') => ' + this.stringify(expr) + ': ' + NodeType.stringify(retType)
head += NodeType.stringify(retType) + ' => '
} else {
return '(' + vars.map(v => this.stringify(v)).join(', ') + ') => ' + this.stringify(expr)
head += ' => '
}

return head + this.stringify(expr)

// // var Var[0], ..., Var[n]
// Vars(vars [Node])
//
/// `external class t extends extend implements implement { fields }`
// TODO IDE: hover over `(className)` in the pattern itself must show its type
case Class(className, extend, implement, fields, external, kind):
return 'class ' + className + ' ' + (extend ? 'extends ' + extend : '') + ' ' + (implement ? 'implements ' + implement : '') + ' {\n' + fields.map(f => this.stringify(f)).join('\n') + '\n}'
return 'class ' + NodeType.stringify(className) + ' ' + (extend ? 'extends ' + extend : '') + (implement.length > 0 ? 'implements ' + implement : '') + ' {\n' + fields.map(f => this.stringify(f)).join('\n') + '\n}'

/// var name T { get { return x } set (v) {} }
// TODO .Var, .Function, .Function
Expand All @@ -140,7 +144,7 @@ class Prettify {
/// }
case Switch(inputs , expressions , guards , patterns ):
return 'switch ' + [for i in inputs.length this.stringify(inputs[i])].join(', ') + ' {\n' +
[for i in patterns.length 'case ' + this.stringify(patterns[i]) + ': ' + this.stringify(expressions[i])].join('\n') + '\n}'
[for i in patterns.length 'case ' + this.stringify(expressions[i]) + ': ' + this.stringify(patterns[i])].join('\n') + '\n}'

/// module path[0].path[1].r..path[n] { el }
case Module(path , el ):
Expand Down Expand Up @@ -217,7 +221,7 @@ class Prettify {
case String(s):
// TODO quote types + mirror special symbols
let s = JSON.stringify(s)
return "'\(s)'"
return s
case Ident(name): return name
case Bool(b): return b ? "true": "false"
case Int(s): return s.toString()
Expand Down Expand Up @@ -255,15 +259,28 @@ class Prettify {
}
return
(external ? 'declare ': '') +
(const ? 'let ': 'var ') + name + ' ' + NodeType.stringify(t) + body
(const ? 'let ': 'var ') + name + (t != null? ' ' + NodeType.stringify(t) : '') + body
}
case Function(name, body, vars, returnType, external, variadic):
if name == null {
// TODO `// Anonymous`
return `fun`
}

return 'fun ' + name
// TODO `new_SPACE_`?
let prefix = name == 'new'? 'new ': 'fun ' + name

var head = prefix + '(' + vars.map(v => this.stringify(v).substr(4)).join(', ') + ')'

if let returnType = returnType {
head += ' ' + NodeType.stringify(returnType)
}

if let body = body {
return head + ' ' + this.stringify(body)
}

return head
case _:
// TODO exhaustive
// console.error('stringify', node)
Expand Down

0 comments on commit 786b7f1

Please sign in to comment.