1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: add v ast file.v (#10236)

This commit is contained in:
lydiandy 2021-05-29 00:54:44 +08:00 committed by GitHub
parent 9ea753e853
commit e09f0234ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 2434 additions and 1 deletions

114
cmd/tools/vast/cjson.v Normal file
View File

@ -0,0 +1,114 @@
module main
import json
struct UseJson {
x int
}
fn suppress_json_warning() {
json.encode(UseJson{})
}
// struct C.cJSON {}
fn C.cJSON_CreateObject() &C.cJSON
fn C.cJSON_CreateArray() &C.cJSON
// fn C.cJSON_CreateBool(bool) &C.cJSON
fn C.cJSON_CreateTrue() &C.cJSON
fn C.cJSON_CreateFalse() &C.cJSON
fn C.cJSON_CreateNull() &C.cJSON
// fn C.cJSON_CreateNumber() &C.cJSON
// fn C.cJSON_CreateString() &C.cJSON
fn C.cJSON_CreateRaw(&byte) &C.cJSON
fn C.cJSON_IsInvalid(voidptr) bool
fn C.cJSON_IsFalse(voidptr) bool
// fn C.cJSON_IsTrue(voidptr) bool
fn C.cJSON_IsBool(voidptr) bool
fn C.cJSON_IsNull(voidptr) bool
fn C.cJSON_IsNumber(voidptr) bool
fn C.cJSON_IsString(voidptr) bool
fn C.cJSON_IsArray(voidptr) bool
fn C.cJSON_IsObject(voidptr) bool
fn C.cJSON_IsRaw(voidptr) bool
fn C.cJSON_AddItemToObject(voidptr, &byte, voidptr)
fn C.cJSON_AddItemToArray(voidptr, voidptr)
fn C.cJSON_Delete(voidptr)
fn C.cJSON_Print(voidptr) &byte
[inline]
fn create_object() &C.cJSON {
return C.cJSON_CreateObject()
}
[inline]
fn create_array() &C.cJSON {
return C.cJSON_CreateArray()
}
[inline]
fn create_string(val string) &C.cJSON {
return C.cJSON_CreateString(val.str)
}
[inline]
fn create_number(val f64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
[inline]
fn create_bool(val bool) &C.cJSON {
return C.cJSON_CreateBool(val)
}
[inline]
fn create_true() &C.cJSON {
return C.cJSON_CreateTrue()
}
[inline]
fn create_false() &C.cJSON {
return C.cJSON_CreateFalse()
}
[inline]
fn create_null() &C.cJSON {
return C.cJSON_CreateNull()
}
[inline]
fn delete(b voidptr) {
C.cJSON_Delete(b)
}
[inline]
fn add_item_to_object(obj &C.cJSON, key string, item &C.cJSON) {
C.cJSON_AddItemToObject(obj, key.str, item)
}
[inline]
fn add_item_to_array(obj &C.cJSON, item &C.cJSON) {
C.cJSON_AddItemToArray(obj, item)
}
fn json_print(json &C.cJSON) string {
s := C.cJSON_Print(json)
return unsafe { tos3(s) }
}

1
cmd/tools/vast/test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
demo.json

110
cmd/tools/vast/test/demo.v Normal file
View File

@ -0,0 +1,110 @@
// usage test: v ast path_to_v/cmd/tools/vast/test/demo.v
// will generate demo.json
// comment for module
module main
// import module
import os
import math
import time { Time, now }
// const decl
const (
a = 1
b = 3
c = 'c'
)
// struct decl
struct Point {
x int
mut:
y int
pub:
z int
pub mut:
name string
}
// method of Point
pub fn (p Point) get_x() int {
return p.x
}
// embed struct
struct MyPoint {
Point
title string
}
// enum type
enum Color {
red
green
blue
}
// type alias
type Myint = int
// sum type
type MySumType = bool | int | string
// function type
type Myfn = fn (int) int
// interface type
interface Myinterfacer {
add(int, int) int
sub(int, int) int
}
// main funciton
fn main() {
add(1, 3)
println(add(1, 2))
println('ok') // comment println
arr := [1, 3, 5, 7]
for a in arr {
println(a)
add(1, 3)
}
color := Color.red
println(color)
println(os.args)
m := math.max(1, 3)
println(m)
println(now())
t := Time{}
println(t)
p := Point{
x: 1
y: 2
z: 3
}
println(p)
my_point := MyPoint{
// x: 1
// y: 3
// z: 5
}
println(my_point.get_x())
}
// normal function
fn add(x int, y int) int {
return x + y
}
// generic function
fn g_fn<T>(p T) T {
return p
}
// generic struct
struct GenericStruct<T> {
point Point
mut:
model T
}

2195
cmd/tools/vast/vast.v Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ import v.util
// should be compiled (v folder).
// To implement that, these folders are initially skipped, then added
// as a whole *after the testing.prepare_test_session call*.
const tools_in_subfolders = ['vdoc', 'vvet']
const tools_in_subfolders = ['vdoc', 'vvet', 'vast']
// non_packaged_tools are tools that should not be packaged with
// prebuild versions of V, to keep the size smaller.

12
cmd/v/help/ast.txt Normal file
View File

@ -0,0 +1,12 @@
Usage:
v ast [options] path_to_source.v
Generate AST json file from v source file.
Options:
-w
generate ast json file, and watch.
-c
generate ast json file and c file, and watch.
-p
print the json string to termial.

View File

@ -36,6 +36,7 @@ const (
'vet',
'wipe-cache',
'watch',
'ast',
]
list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
)