2019-07-29 18:21:36 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
import strings
|
2019-11-09 20:05:44 +01:00
|
|
|
import os
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-08-07 08:19:27 +02:00
|
|
|
fn (scanner mut Scanner) fgen(s_ string) {
|
2019-08-17 21:19:37 +02:00
|
|
|
mut s := s_
|
2019-07-29 18:21:36 +02:00
|
|
|
if scanner.fmt_line_empty {
|
|
|
|
s = strings.repeat(`\t`, scanner.fmt_indent) + s
|
|
|
|
}
|
|
|
|
scanner.fmt_out.write(s)
|
|
|
|
scanner.fmt_line_empty = false
|
|
|
|
}
|
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-08-07 08:19:27 +02:00
|
|
|
fn (scanner mut Scanner) fgenln(s_ string) {
|
2019-08-17 21:19:37 +02:00
|
|
|
mut s := s_
|
2019-07-29 18:21:36 +02:00
|
|
|
if scanner.fmt_line_empty {
|
|
|
|
s = strings.repeat(`\t`, scanner.fmt_indent) + s
|
|
|
|
}
|
|
|
|
scanner.fmt_out.writeln(s)
|
|
|
|
scanner.fmt_line_empty = true
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:13:26 +01:00
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) fgen(s string) {
|
2019-11-09 17:13:26 +01:00
|
|
|
}
|
|
|
|
[if vfmt]
|
|
|
|
fn (p mut Parser) fgen2(s string) {
|
|
|
|
if p.pass != .main {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.scanner.fgen(s)
|
|
|
|
}
|
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) fspace() {
|
2019-11-09 17:13:26 +01:00
|
|
|
if p.first_pass() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.fgen2(' ')
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:13:26 +01:00
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) fgenln(s string) {
|
2019-11-09 17:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[if vfmt]
|
|
|
|
fn (p mut Parser) fgenln2(s string) {
|
|
|
|
if p.pass != .main {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.scanner.fgenln(s)
|
|
|
|
}
|
|
|
|
|
2019-09-27 03:00:48 +02:00
|
|
|
/*
|
2019-10-09 00:05:34 +02:00
|
|
|
fn (p mut Parser) peek() TokenKind {
|
2019-07-29 18:21:36 +02:00
|
|
|
for {
|
2019-08-04 11:00:56 +02:00
|
|
|
p.cgen.line = p.scanner.line_nr + 1
|
2019-07-29 18:21:36 +02:00
|
|
|
tok := p.scanner.peek()
|
|
|
|
if tok != .nl {
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 09:49:56 +02:00
|
|
|
return .eof // TODO can never get here - v doesn't know that
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
2019-09-27 03:00:48 +02:00
|
|
|
*/
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) fmt_inc() {
|
2019-11-09 17:13:26 +01:00
|
|
|
if p.pass != .main {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.scanner.fmt_indent++
|
|
|
|
}
|
|
|
|
|
2019-10-27 10:16:33 +01:00
|
|
|
[if vfmt]
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) fmt_dec() {
|
2019-11-09 17:13:26 +01:00
|
|
|
if p.pass != .main {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.scanner.fmt_indent--
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:13:26 +01:00
|
|
|
[if vfmt]
|
|
|
|
fn (p mut Parser) fnext() {
|
|
|
|
if p.tok == .eof {
|
|
|
|
return
|
|
|
|
}
|
2019-11-09 20:05:44 +01:00
|
|
|
if p.tok == .rcbr && !p.inside_if_expr {
|
2019-11-09 17:13:26 +01:00
|
|
|
p.fmt_dec()
|
|
|
|
}
|
|
|
|
p.fgen2(p.strtok())
|
|
|
|
// vfmt: increase indentation on `{` unless it's `{}`
|
2019-11-09 20:05:44 +01:00
|
|
|
if p.tok == .lcbr && !p.inside_if_expr { //&& p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
|
2019-11-09 17:13:26 +01:00
|
|
|
p.fgenln2('')
|
|
|
|
p.fmt_inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 20:05:44 +01:00
|
|
|
|
|
|
|
[if vfmt]
|
|
|
|
fn (p mut Parser) gen_fmt() {
|
|
|
|
if p.pass != .main {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.file_name == '' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := p.scanner.fmt_out.str().trim_space()
|
|
|
|
if s == '' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println('GENERATING ${p.file_name}.V')
|
|
|
|
out := os.create('/var/tmp/fmt/' + p.file_name) or {
|
|
|
|
verror('failed to create fmt.v')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//println(p.scanner.fmt_out.str())
|
|
|
|
out.writeln(p.scanner.fmt_out.str().trim_space())
|
|
|
|
out.close()
|
|
|
|
}
|
|
|
|
|