-
Notifications
You must be signed in to change notification settings - Fork 6
/
value.h
124 lines (102 loc) · 3 KB
/
value.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
value.h
SuperCalc
Created by C0deH4cker on 10/21/13.
Copyright (c) 2013 C0deH4cker. All rights reserved.
*/
#ifndef SC_VALUE_H
#define SC_VALUE_H
#include <stdbool.h>
#include "annotations.h"
typedef struct Value Value;
/* Callback and associated data */
typedef struct parser_cb {
Value* _Nonnull (*_Nonnull func)(istring expr, void* _Nullable data);
void* _Nullable data;
} parser_cb;
#include "fraction.h"
#include "unop.h"
#include "binop.h"
#include "funccall.h"
#include "variable.h"
#include "context.h"
#include "error.h"
#include "generic.h"
#include "vector.h"
#include "function.h"
#include "builtin.h"
#include "placeholder.h"
ASSUME_NONNULL_BEGIN
typedef enum {
VAL_APPROX = -4,
VAL_END = -3,
VAL_ERR = -2,
VAL_NEG = -1,
VAL_INT = 0,
VAL_REAL,
VAL_FRAC,
VAL_EXPR,
VAL_UNARY,
VAL_CALL,
VAL_VAR,
VAL_VEC,
VAL_FUNC,
VAL_BUILTIN,
VAL_PLACE
} VALTYPE;
struct Value {
VALTYPE type;
union {
long long ival;
double rval;
OWNED Fraction* frac;
OWNED Vector* vec;
OWNED UnOp* term;
OWNED BinOp* expr;
OWNED FuncCall* call;
OWNED char* name;
OWNED Error* err;
OWNED Function* func;
OWNED Builtin* blt;
OWNED Placeholder* ph;
};
};
/* Default parser callback errors on '@' */
extern parser_cb default_cb;
/* Value constructors */
RETURNS_OWNED Value* ValEnd(void);
RETURNS_OWNED Value* ValErr(CONSUMED Error* err);
RETURNS_OWNED Value* ValNeg(void);
RETURNS_OWNED Value* ValInt(long long val);
RETURNS_OWNED Value* ValReal(double val);
RETURNS_OWNED Value* ValFrac(CONSUMED Fraction* frac);
RETURNS_OWNED Value* ValExpr(CONSUMED BinOp* expr);
RETURNS_OWNED Value* ValUnary(CONSUMED UnOp* term);
RETURNS_OWNED Value* ValCall(CONSUMED FuncCall* call);
RETURNS_OWNED Value* ValVar(CONSUMED char* name);
RETURNS_OWNED Value* ValVec(CONSUMED Vector* vec);
RETURNS_OWNED Value* ValFunc(CONSUMED Function* func);
RETURNS_OWNED Value* ValBuiltin(CONSUMED Builtin* blt);
RETURNS_OWNED Value* ValPlace(CONSUMED Placeholder* ph);
/* Destructor */
void Value_free(CONSUMED Value* _Nullable val);
/* Copying */
RETURNS_OWNED Value* Value_copy(const Value* val);
/* Evaluation */
RETURNS_OWNED Value* Value_eval(const Value* val, const Context* ctx);
RETURNS_OWNED Value* Value_coerce(const Value* val, const Context* ctx);
bool Value_isCallable(const Value* val);
/* Conversion */
double Value_asReal(const Value* val);
/* Parsing */
RETURNS_OWNED Value* Value_parseTop(INOUT istring expr);
RETURNS_OWNED Value* Value_parse(INOUT istring expr, char sep, char end, parser_cb* cb);
RETURNS_OWNED Value* Value_next(INOUT istring expr, char sep, char end, parser_cb* cb);
/* Printing */
RETURNS_OWNED char* Value_repr(const Value* val, bool pretty, bool top);
RETURNS_OWNED char* Value_wrap(const Value* val, bool top);
RETURNS_OWNED char* Value_verbose(const Value* val, unsigned indent);
RETURNS_OWNED char* Value_xml(const Value* val, unsigned indent);
void Value_print(const Value* val, VERBOSITY v);
ASSUME_NONNULL_END
#endif /* SC_VALUE_H */