2020-02-21 18:13:34 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-02-19 16:12:39 +01:00
|
|
|
module ast
|
2020-02-21 18:13:34 +01:00
|
|
|
/*
|
|
|
|
These methods are used only by vfmt, vdoc, and for debugging.
|
|
|
|
*/
|
|
|
|
|
2020-02-19 16:12:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
v.table
|
|
|
|
strings
|
|
|
|
)
|
|
|
|
|
|
|
|
pub fn (node &FnDecl) str(t &table.Table) string {
|
|
|
|
mut f := strings.new_builder(30)
|
2020-02-29 17:51:35 +01:00
|
|
|
if node.is_pub {
|
|
|
|
f.write('pub ')
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
mut receiver := ''
|
|
|
|
if node.is_method {
|
|
|
|
sym := t.get_type_symbol(node.receiver.typ)
|
|
|
|
name := sym.name.after('.')
|
|
|
|
m := if node.rec_mut { 'mut ' } else { '' }
|
2020-02-29 15:04:07 +01:00
|
|
|
receiver = '($node.receiver.name $m$name) '
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-02-22 16:59:50 +01:00
|
|
|
name := node.name.after('.')
|
|
|
|
f.write('fn ${receiver}${name}(')
|
2020-02-19 16:12:39 +01:00
|
|
|
for i, arg in node.args {
|
2020-03-11 01:31:24 +01:00
|
|
|
// skip receiver
|
|
|
|
if node.is_method && i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
is_last_arg := i == node.args.len - 1
|
2020-02-29 15:04:07 +01:00
|
|
|
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ ||
|
|
|
|
(node.is_variadic && i == node.args.len - 2)
|
2020-02-19 16:12:39 +01:00
|
|
|
f.write(arg.name)
|
|
|
|
if should_add_type {
|
2020-02-29 15:04:07 +01:00
|
|
|
if node.is_variadic && is_last_arg {
|
|
|
|
f.write(' ...' + t.type_to_str(arg.typ))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.write(' ' + t.type_to_str(arg.typ))
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
if !is_last_arg {
|
|
|
|
f.write(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.write(')')
|
2020-03-11 16:10:46 +01:00
|
|
|
if node.return_type != table.void_type {
|
2020-02-28 17:21:20 +01:00
|
|
|
// typ := t.type_to_str(node.typ)
|
|
|
|
// if typ.starts_with('
|
2020-03-11 16:10:46 +01:00
|
|
|
f.write(' ' + t.type_to_str(node.return_type))
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
return f.str()
|
|
|
|
}
|
2020-02-27 21:29:38 +01:00
|
|
|
|
|
|
|
// string representaiton of expr
|
|
|
|
pub fn (x Expr) str() string {
|
|
|
|
match x {
|
2020-03-26 19:17:14 +01:00
|
|
|
Ident {
|
|
|
|
return it.name
|
|
|
|
}
|
2020-02-27 21:29:38 +01:00
|
|
|
InfixExpr {
|
|
|
|
return '(${it.left.str()} $it.op.str() ${it.right.str()})'
|
|
|
|
}
|
|
|
|
PrefixExpr {
|
2020-04-02 21:31:36 +02:00
|
|
|
return it.op.str() + it.right.str()
|
2020-02-27 21:29:38 +01:00
|
|
|
}
|
|
|
|
IntegerLiteral {
|
2020-03-17 02:49:15 +01:00
|
|
|
return it.val
|
2020-02-27 21:29:38 +01:00
|
|
|
}
|
2020-03-17 02:49:15 +01:00
|
|
|
StringLiteral {
|
2020-02-27 21:29:38 +01:00
|
|
|
return '"$it.val"'
|
|
|
|
}
|
2020-04-02 21:31:36 +02:00
|
|
|
ParExpr {
|
|
|
|
return it.expr.str()
|
|
|
|
}
|
2020-04-02 23:42:08 +02:00
|
|
|
IndexExpr {
|
|
|
|
return '${it.left.str()}[${it.index.str()}]'
|
|
|
|
}
|
2020-02-27 21:29:38 +01:00
|
|
|
else {
|
2020-04-02 21:31:36 +02:00
|
|
|
return '[unhandled expr type ${typeof(x)}]'
|
2020-02-27 21:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (node Stmt) str() string {
|
|
|
|
match node {
|
2020-03-10 12:01:37 +01:00
|
|
|
AssignStmt {
|
|
|
|
mut out := ''
|
|
|
|
for i,ident in it.left {
|
|
|
|
var_info := ident.var_info()
|
|
|
|
if var_info.is_mut {
|
|
|
|
out += 'mut '
|
|
|
|
}
|
|
|
|
out += ident.name
|
|
|
|
if i < it.left.len-1 {
|
|
|
|
out += ','
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out += ' $it.op.str() '
|
|
|
|
for i,val in it.right {
|
|
|
|
out += val.str()
|
|
|
|
if i < it.right.len-1 {
|
|
|
|
out += ','
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
2020-02-27 21:29:38 +01:00
|
|
|
}
|
|
|
|
ExprStmt {
|
|
|
|
return it.expr.str()
|
|
|
|
}
|
|
|
|
FnDecl {
|
|
|
|
return 'fn ${it.name}() { $it.stmts.len stmts }'
|
|
|
|
}
|
|
|
|
else {
|
2020-04-02 21:31:36 +02:00
|
|
|
return '[unhandled stmt str type: ${typeof(node)} ]'
|
2020-02-27 21:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|