This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ast.go
86 lines (75 loc) · 2.14 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package z3
// #include <stdlib.h>
// #include "go-z3.h"
import "C"
// AST represents an AST value in Z3.
//
// AST memory management is automatically managed by the Context it
// is contained within. When the Context is freed, so are the AST nodes.
type AST struct {
rawCtx C.Z3_context
rawAST C.Z3_ast
}
// String returns a human-friendly string version of the AST.
func (a *AST) String() string {
return C.GoString(C.Z3_ast_to_string(a.rawCtx, a.rawAST))
}
// DeclName returns the name of a declaration. The AST value must be a
// func declaration for this to work.
func (a *AST) DeclName() *Symbol {
return &Symbol{
rawCtx: a.rawCtx,
rawSymbol: C.Z3_get_decl_name(
a.rawCtx, C.Z3_to_func_decl(a.rawCtx, a.rawAST)),
}
}
//-------------------------------------------------------------------
// Var, Literal Creation
//-------------------------------------------------------------------
// Const declares a variable. It is called "Const" since internally
// this is equivalent to create a function that always returns a constant
// value. From an initial user perspective this may be confusing but go-z3
// is following identical naming convention.
func (c *Context) Const(s *Symbol, typ *Sort) *AST {
return &AST{
rawCtx: c.raw,
rawAST: C.Z3_mk_const(c.raw, s.rawSymbol, typ.rawSort),
}
}
// Int creates an integer type.
//
// Maps: Z3_mk_int
func (c *Context) Int(v int, typ *Sort) *AST {
return &AST{
rawCtx: c.raw,
rawAST: C.Z3_mk_int(c.raw, C.int(v), typ.rawSort),
}
}
// True creates the value "true".
//
// Maps: Z3_mk_true
func (c *Context) True() *AST {
return &AST{
rawCtx: c.raw,
rawAST: C.Z3_mk_true(c.raw),
}
}
// False creates the value "false".
//
// Maps: Z3_mk_false
func (c *Context) False() *AST {
return &AST{
rawCtx: c.raw,
rawAST: C.Z3_mk_false(c.raw),
}
}
//-------------------------------------------------------------------
// Value Readers
//-------------------------------------------------------------------
// Int gets the integer value of this AST. The value must be able to fit
// into a machine integer.
func (a *AST) Int() int {
var dst C.int
C.Z3_get_numeral_int(a.rawCtx, a.rawAST, &dst)
return int(dst)
}