Skip to content

v2.ast #

Constants #

const empty_expr = Expr(EmptyExpr(0))
const empty_stmt = Stmt(EmptyStmt(0))

fn StringInterFormat.from_u8 #

fn StringInterFormat.from_u8(c u8) !StringInterFormat

fn StringLiteralKind.from_string_tinyv #

fn StringLiteralKind.from_string_tinyv(s string) !StringLiteralKind

Todo: allow overriding this method in main v compilerthat is why this method was renamed from from_string

type Expr #

type Expr = ArrayInitExpr
	| AsCastExpr
	| AssocExpr
	| BasicLiteral
	| CallExpr
	| CallOrCastExpr
	| CastExpr
	| ComptimeExpr
	| EmptyExpr
	| FieldInit
	| FnLiteral
	| GenericArgOrIndexExpr
	| GenericArgs
	| Ident
	| IfExpr
	| IfGuardExpr
	| IndexExpr
	| InfixExpr
	| InitExpr
	| Keyword
	| KeywordOperator
	| LambdaExpr
	| LockExpr
	| MapInitExpr
	| MatchExpr
	| ModifierExpr
	| OrExpr
	| ParenExpr
	| PostfixExpr
	| PrefixExpr
	| RangeExpr
	| SelectExpr
	| SelectorExpr
	| SqlExpr
	| StringInterLiteral
	| StringLiteral
	| Tuple
	| Type
	| UnsafeExpr

fn (Expr) name #

fn (expr Expr) name() string

Todo: fix this, should be only what is needed

fn (Expr) pos #

fn (expr Expr) pos() token.Pos

type Stmt #

type Stmt = AsmStmt
	| AssertStmt
	| AssignStmt
	| BlockStmt
	| ComptimeStmt
	| ConstDecl
	| DeferStmt
	| Directive
	| EmptyStmt
	| EnumDecl
	| ExprStmt
	| FlowControlStmt
	| FnDecl
	| ForInStmt
	| ForStmt
	| GlobalDecl
	| ImportStmt
	| InterfaceDecl
	| LabelStmt
	| ModuleStmt
	| ReturnStmt
	| StructDecl
	| TypeDecl
	| []Attribute

Todo: decide if this going to be done like this (FieldInit)

type Type #

type Type = AnonStructType
	| ArrayFixedType
	| ArrayType
	| ChannelType
	| FnType
	| GenericType
	| MapType
	| NilType
	| NoneType
	| OptionType
	| ResultType
	| ThreadType
	| TupleType

Todo: (re)implement nested sumtype like TS (was removed from v)currently need to cast to type in parser.type. Should I leave like this or add these directly to Expr until nesting is implemented?

fn (Type) name #

fn (t Type) name() string

fn ([]Attribute) has #

fn (attributes []Attribute) has(name string) bool

fn ([]Expr) name_list #

fn (exprs []Expr) name_list() string

enum Language #

enum Language {
	v
	c
	js
}

fn (Language) str #

fn (lang Language) str() string

enum StringInterFormat #

enum StringInterFormat {
	unformatted
	binary
	character
	decimal
	exponent
	exponent_short
	float
	hex
	octal
	pointer_address
	string
}

fn (StringInterFormat) str #

fn (sif StringInterFormat) str() string

enum StringLiteralKind #

enum StringLiteralKind {
	c
	js
	raw
	v
}

fn (StringLiteralKind) str #

fn (s StringLiteralKind) str() string

struct AnonStructType #

struct AnonStructType {
pub:
	generic_params []Expr
	embedded       []Expr
	fields         []FieldDecl
}

pub fn (expr Expr) str() string { return match expr { Ident { expr.name } SelectorExpr { expr.lhs.str() + '.' + expr.rhs.name } else { 'missing Expr.str() for ${expr.type_name()}' // expr.str() } } }

struct ArrayFixedType #

struct ArrayFixedType {
pub:
	len       Expr
	elem_type Expr
}

struct ArrayInitExpr #

struct ArrayInitExpr {
pub:
	typ   Expr = ast.empty_expr
	exprs []Expr
	init  Expr = ast.empty_expr
	cap   Expr = ast.empty_expr
	len   Expr = ast.empty_expr
	pos   token.Pos
}

Expressions

struct ArrayType #

struct ArrayType {
pub:
	elem_type Expr
}

Type Nodes

struct AsCastExpr #

struct AsCastExpr {
pub:
	expr Expr
	typ  Expr
	pos  token.Pos
}

struct AsmStmt #

struct AsmStmt {
pub:
	arch string
}

Statements

struct AssertStmt #

struct AssertStmt {
pub:
	expr  Expr
	extra Expr = ast.empty_expr
}

struct AssignStmt #

struct AssignStmt {
pub:
	op  token.Token
	lhs []Expr
	rhs []Expr
	pos token.Pos
}

struct AssocExpr #

struct AssocExpr {
pub:
	typ    Expr
	expr   Expr
	fields []FieldInit
}

struct Attribute #

struct Attribute {
pub:
	name          string
	value         Expr
	comptime_cond Expr
}

struct BasicLiteral #

struct BasicLiteral {
pub:
	kind  token.Token
	value string
}

struct BlockStmt #

struct BlockStmt {
pub:
	stmts []Stmt
}

struct CallExpr #

struct CallExpr {
pub:
	lhs  Expr
	args []Expr
	pos  token.Pos
}

struct CallOrCastExpr #

struct CallOrCastExpr {
pub:
	lhs  Expr
	expr Expr
	pos  token.Pos
}

struct CastExpr #

struct CastExpr {
pub:
	typ  Expr
	expr Expr
	pos  token.Pos
}

struct ChannelType #

struct ChannelType {
pub:
	cap       Expr
	elem_type Expr
}

struct ComptimeExpr #

struct ComptimeExpr {
pub:
	expr Expr
	pos  token.Pos
}

struct ComptimeStmt #

struct ComptimeStmt {
pub:
	stmt Stmt
}

struct ConstDecl #

struct ConstDecl {
pub:
	is_public bool
	fields    []FieldInit
}

struct DeferStmt #

struct DeferStmt {
pub:
	stmts []Stmt
}

struct Directive #

struct Directive {
pub:
	name  string
	value string
}

#flag / #include

struct EnumDecl #

struct EnumDecl {
pub:
	attributes []Attribute
	is_public  bool
	name       string
	as_type    Expr = ast.empty_expr
	fields     []FieldDecl
}

struct ExprStmt #

struct ExprStmt {
pub:
	expr Expr
}

struct FieldDecl #

struct FieldDecl {
pub:
	name       string
	typ        Expr = ast.empty_expr // can be empty as used for const (unless we use something else)
	value      Expr = ast.empty_expr
	attributes []Attribute
}

struct FieldInit #

struct FieldInit {
pub:
	name  string
	value Expr
}

struct File #

struct File {
pub:
	attributes []Attribute
	mod        string
	name       string
	stmts      []Stmt
	imports    []ImportStmt
}

File (AST container)

struct FlowControlStmt #

struct FlowControlStmt {
pub:
	op    token.Token
	label string
}

struct FnDecl #

struct FnDecl {
pub:
	attributes []Attribute
	is_public  bool
	is_method  bool
	is_static  bool
	receiver   Parameter
	language   Language = .v
	name       string
	typ        FnType
	stmts      []Stmt
	pos        token.Pos
}

enum FnArributes { method static }

struct FnLiteral #

struct FnLiteral {
pub:
	typ           FnType
	captured_vars []Expr
	stmts         []Stmt
}

anon fn / closure

struct FnType #

struct FnType {
pub:
	generic_params []Expr
	params         []Parameter
	return_type    Expr = ast.empty_expr
}

fn (FnType) str #

fn (ft &FnType) str() string

struct ForInStmt #

struct ForInStmt {
pub:
	// key   		 string
	// value 		 string
	// value_is_mut bool
	// expr	     Expr
	// TODO:
	key   Expr = ast.empty_expr
	value Expr
	expr  Expr
}

Note: used as the initializer for ForStmt

struct ForStmt #

struct ForStmt {
pub:
	init  Stmt = ast.empty_stmt // initialization
	cond  Expr = ast.empty_expr // condition
	post  Stmt = ast.empty_stmt // post iteration (afterthought)
	stmts []Stmt
}

struct GenericArgOrIndexExpr #

struct GenericArgOrIndexExpr {
pub:
	lhs  Expr
	expr Expr
}

struct GenericArgs #

struct GenericArgs {
pub:
	lhs  Expr
	args []Expr // concrete types
}

struct GenericType #

struct GenericType {
pub:
	name   Expr
	params []Expr
}

struct GlobalDecl #

struct GlobalDecl {
pub:
	attributes []Attribute
	fields     []FieldDecl
}

struct Ident #

struct Ident {
pub:
	pos  token.Pos
	name string
}

fn (Ident) str #

fn (ident &Ident) str() string

struct IfExpr #

struct IfExpr {
pub:
	cond      Expr = ast.empty_expr
	else_expr Expr = ast.empty_expr
	stmts     []Stmt
}

struct IfGuardExpr #

struct IfGuardExpr {
pub:
	stmt AssignStmt
}

struct ImportStmt #

struct ImportStmt {
pub:
	name       string
	alias      string
	is_aliased bool
	symbols    []Expr
}

struct IndexExpr #

struct IndexExpr {
pub:
	lhs      Expr
	expr     Expr
	is_gated bool
}

struct InfixExpr #

struct InfixExpr {
pub:
	op  token.Token
	lhs Expr
	rhs Expr
	pos token.Pos
}

struct InitExpr #

struct InitExpr {
pub:
	typ    Expr
	fields []FieldInit
}

struct InterfaceDecl #

struct InterfaceDecl {
pub:
	is_public      bool
	attributes     []Attribute
	name           string
	generic_params []Expr
	embedded       []Expr
	fields         []FieldDecl
}

struct Keyword #

struct Keyword {
pub:
	tok token.Token
}

struct KeywordOperator #

struct KeywordOperator {
pub:
	op    token.Token
	exprs []Expr
}

struct LabelStmt #

struct LabelStmt {
pub:
	name string
	stmt Stmt = ast.empty_stmt
}

struct LambdaExpr #

struct LambdaExpr {
pub:
	args []Ident
	expr Expr
}

struct LockExpr #

struct LockExpr {
pub:
	lock_exprs  []Expr
	rlock_exprs []Expr
	stmts       []Stmt
}

struct MapInitExpr #

struct MapInitExpr {
pub:
	typ  Expr = ast.empty_expr
	keys []Expr
	vals []Expr
	pos  token.Pos
}

struct MapType #

struct MapType {
pub:
	key_type   Expr
	value_type Expr
}

struct MatchBranch #

struct MatchBranch {
pub:
	cond  []Expr
	stmts []Stmt
	pos   token.Pos
}

struct MatchExpr #

struct MatchExpr {
pub:
	expr     Expr
	branches []MatchBranch
	pos      token.Pos
}

fn (MatchExpr) desugar #

fn (match_expr &MatchExpr) desugar() Expr

Note: this is just a very naive example of how it could possibly work.actual implementation may work during AST -> IR (or not). it may also need type information which we don't have here. as I said, just an example.

struct ModifierExpr #

struct ModifierExpr {
pub:
	kind token.Token
	expr Expr
}

Todo: possibly merge modifiers into a bitfield wheremultiple modifiers are used. this could also be used for struct decl mut:, pub mut: etc. consider this [flag] pub enum Modifier { .atomic .global .mutable .public .shared .static .volatile }

struct ModuleStmt #

struct ModuleStmt {
pub:
	name string
}

struct NilType #

struct NilType {}

struct NoneType #

struct NoneType {}

struct OptionType #

struct OptionType {
pub:
	base_type Expr = ast.empty_expr
}

struct OrExpr #

struct OrExpr {
pub:
	expr  Expr
	stmts []Stmt
	pos   token.Pos
}

pub fn (expr ModifierExpr) unwrap() Expr { return expr.expr }

fn (OrExpr) desugar #

fn (or_expr &OrExpr) desugar() Expr

struct Parameter #

struct Parameter {
pub:
	name   string
	typ    Expr
	is_mut bool
	pos    token.Pos
}

struct ParenExpr #

struct ParenExpr {
pub:
	expr Expr
}

struct PostfixExpr #

struct PostfixExpr {
pub:
	op   token.Token
	expr Expr
}

struct PrefixExpr #

struct PrefixExpr {
pub:
	op   token.Token
	expr Expr
	pos  token.Pos
}

struct RangeExpr #

struct RangeExpr {
pub:
	op    token.Token // `..` exclusive | `...` inclusive
	start Expr
	end   Expr
}

struct ResultType #

struct ResultType {
pub:
	base_type Expr = ast.empty_expr
}

struct ReturnStmt #

struct ReturnStmt {
pub:
	exprs []Expr
}

struct SelectExpr #

struct SelectExpr {
pub:
	pos   token.Pos
	stmt  Stmt
	stmts []Stmt
	next  Expr = ast.empty_expr
}

struct SelectorExpr #

struct SelectorExpr {
pub:
	lhs Expr
	rhs Ident
	pos token.Pos
}

fn (SelectorExpr) leftmost #

fn (se SelectorExpr) leftmost() Expr

fn (SelectorExpr) name #

fn (se SelectorExpr) name() string

pub fn (expr Expr) str() string { return 'Expr.str() - $expr.type_name()' }

struct SqlExpr #

struct SqlExpr {
pub:
	expr Expr
}

struct StringInter #

struct StringInter {
pub:
	format    StringInterFormat
	width     int
	precision int
	expr      Expr
	// TEMP: prob removed once individual
	// fields are set, precision etc
	format_expr Expr = ast.empty_expr
}

struct StringInterLiteral #

struct StringInterLiteral {
pub:
	kind   StringLiteralKind
	values []string
	inters []StringInter
}

struct StringLiteral #

struct StringLiteral {
pub:
	kind  StringLiteralKind
	value string
}

Note: I'm using two nodes StringLiteral & StringInterLiteralto avoid the extra array allocations when not needed.

struct StructDecl #

struct StructDecl {
pub:
	attributes     []Attribute
	is_public      bool
	embedded       []Expr
	language       Language = .v
	name           string
	generic_params []Expr
	fields         []FieldDecl
	pos            token.Pos
}

struct ThreadType #

struct ThreadType {
pub:
	elem_type Expr = ast.empty_expr
}

struct Tuple #

struct Tuple {
pub:
	exprs []Expr
}

struct TupleType #

struct TupleType {
pub:
	types []Expr
}

struct TypeDecl #

struct TypeDecl {
pub:
	is_public      bool
	language       Language
	name           string
	generic_params []Expr
	base_type      Expr = ast.empty_expr
	variants       []Expr
}

struct UnsafeExpr #

struct UnsafeExpr {
pub:
	stmts []Stmt
}