Skip to content

Commit

Permalink
check-md: verify code example formatting (vlang#7143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Neubert authored Dec 5, 2020
1 parent 0d28f12 commit 8adb1ac
Show file tree
Hide file tree
Showing 14 changed files with 835 additions and 751 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
## Key Features of V

- Simplicity: the language can be learned in less than an hour
- Fast compilation: ≈80k loc/s with a Clang backend,
- Fast compilation: ≈80k loc/s with a Clang backend,
≈1 million loc/s with x64 and tcc backends *(Intel i5-7500, SSD, no optimization)*
- Easy to develop: V compiles itself in less than a second
- Performance: as fast as C (V's main backend compiles to human readable C)
Expand Down Expand Up @@ -81,7 +81,7 @@ v up

### C compiler

It's recommended to use Clang or GCC or Visual Studio.
It's recommended to use Clang or GCC or Visual Studio.
If you are doing development, you most likely already have one of those installed.

Otherwise, follow these instructions:
Expand All @@ -90,8 +90,8 @@ Otherwise, follow these instructions:

- [Installing a C compiler on Windows](https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows)

However, if none is found when running `make` on Linux or Windows,
TCC would be downloaded and set as an alternative C backend.
However, if none is found when running `make` on Linux or Windows,
TCC would be downloaded and set as an alternative C backend.
It's very lightweight (several MB) so this shouldn't take too long.

### Symlinking
Expand Down Expand Up @@ -227,9 +227,9 @@ https://github.com/vlang/ui
```v
fn main() {
for i in 0..3 {
println('Hello from V.js')
}
for i in 0 .. 3 {
println('Hello from V.js')
}
}
```
Expand Down
35 changes: 27 additions & 8 deletions cmd/tools/check-md.v
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ fn eline(file_path string, lnumber int, column int, message string) string {
return btext('$file_path:${lnumber + 1}:${column + 1}:') + btext(rtext(' error: $message'))
}

//
const (
default_command = 'compile'
)
Expand Down Expand Up @@ -190,16 +189,23 @@ fn (mut f MDFile) check_examples() (int, int) {
vfile := os.join_path(os.temp_dir(), 'check_${fname}_example_${e.sline}__${e.eline}__${uid}.v')
mut should_cleanup_vfile := true
// eprintln('>>> checking example $vfile ...')
vcontent := e.text.join('\n')
vcontent := e.text.join('\n') + '\n'
os.write_file(vfile, vcontent) or { panic(err) }
mut acommands := e.command.split(' ')
nofmt := 'nofmt' in acommands
for command in acommands {
match command {
'compile' {
res := os.system('"$vexe" -w -Wfatal-errors -o x.c $vfile')
os.rm('x.c') or { }
if res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example failed to compile'))
fmt_res := if nofmt { 0 } else { os.system('"$vexe" fmt -verify $vfile') }
if res != 0 || fmt_res != 0 {
if res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example failed to compile'))
}
if fmt_res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example is not formatted'))
}
eprintln(vcontent)
should_cleanup_vfile = false
errors++
Expand All @@ -209,8 +215,14 @@ fn (mut f MDFile) check_examples() (int, int) {
}
'live' {
res := os.system('"$vexe" -w -Wfatal-errors -live -o x.c $vfile')
if res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example failed to compile with -live'))
fmt_res := if nofmt { 0 } else { os.system('"$vexe" fmt -verify $vfile') }
if res != 0 || fmt_res != 0 {
if res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example failed to compile with -live'))
}
if fmt_res != 0 {
eprintln(eline(f.path, e.sline, 0, 'example is not formatted'))
}
eprintln(vcontent)
should_cleanup_vfile = false
errors++
Expand All @@ -232,8 +244,14 @@ fn (mut f MDFile) check_examples() (int, int) {
}
'oksyntax' {
res := os.system('"$vexe" -w -Wfatal-errors -check-syntax $vfile')
if res != 0 {
eprintln(eline(f.path, e.sline, 0, '`oksyntax` example with invalid syntax'))
fmt_res := if nofmt { 0 } else { os.system('"$vexe" fmt -verify $vfile') }
if res != 0 || fmt_res != 0 {
if res != 0 {
eprintln(eline(f.path, e.sline, 0, '`oksyntax` example with invalid syntax'))
}
if fmt_res != 0 {
eprintln(eline(f.path, e.sline, 0, '`oksyntax` example is not formatted'))
}
eprintln(vcontent)
should_cleanup_vfile = false
errors++
Expand All @@ -252,6 +270,7 @@ fn (mut f MDFile) check_examples() (int, int) {
}
oks++
}
'nofmt' {}
else {
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/failcompile/oksyntax/badsyntax'))
should_cleanup_vfile = false
Expand Down
Loading

0 comments on commit 8adb1ac

Please sign in to comment.