Skip to content

Commit 8160ba9

Browse files
committed
Merge pull request #2045 from mgreter/documentation/sass_value_api
Improve `Sass_Value` documentation
2 parents 45d0795 + f4ae94f commit 8160ba9

File tree

5 files changed

+138
-50
lines changed

5 files changed

+138
-50
lines changed

docs/api-doc.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ char* sass_resolve_file (const char* path, const char* incs[]);
130130
131131
// Get compiled libsass version
132132
const char* libsass_version(void);
133+
134+
// Implemented sass language version
135+
// Hardcoded version 3.4 for time being
136+
const char* libsass_language_version(void);
133137
```
134138

135139
## Common Pitfalls

docs/api-value-example.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Example operation.c
2+
3+
```C
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include "sass/values.h"
7+
8+
int main( int argc, const char* argv[] )
9+
{
10+
11+
// create two new sass values to be added
12+
union Sass_Value* string = sass_make_string("String");
13+
union Sass_Value* number = sass_make_number(42, "nits");
14+
15+
// invoke the add operation which returns a new sass value
16+
union Sass_Value* total = sass_value_op(ADD, string, number);
17+
18+
// no further use for the two operands
19+
sass_delete_value(string);
20+
sass_delete_value(number);
21+
22+
// this works since libsass will always return a
23+
// string for add operations with a string as the
24+
// left hand side. But you should never rely on it!
25+
puts(sass_string_get_value(total));
26+
27+
// invoke stringification (uncompressed with precision of 5)
28+
union Sass_Value* result = sass_value_stringify(total, false, 5);
29+
30+
// no further use for the sum
31+
sass_delete_value(total);
32+
33+
// print the result - you may want to make
34+
// sure result is indeed a string, altough
35+
// stringify guarantees to return a string
36+
// if (sass_value_is_string(result)) {}
37+
// really depends on your level of paranoia
38+
puts(sass_string_get_value(result));
39+
40+
// finally free result
41+
sass_delete_value(result);
42+
43+
// exit status
44+
return 0;
45+
46+
}
47+
```
48+
49+
## Compile operation.c
50+
51+
```bash
52+
gcc -c operation.c -o operation.o
53+
gcc -o operation operation.o -lsass
54+
./operation # => String42nits
55+
```

docs/api-value.md

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
`Sass_Values` are used to pass values and their types between the implementer and LibSass. Sass knows various different value types (including nested arrays and hash-maps). If you implement a binding to another programming language, you have to find a way to convert `Sass_Values` between the targeted language and C. `Sass_Values` are currently only used by custom functions.
1+
`Sass_Values` are used to pass values and their types between the implementer
2+
and LibSass. Sass knows various different value types (including nested arrays
3+
and hash-maps). If you implement a binding to another programming language, you
4+
have to find a way to [marshal] [1] (convert) `Sass_Values` between the target
5+
language and C. `Sass_Values` are currently only used by custom functions, but
6+
it should also be possible to use them without a compiler context.
7+
8+
[1]: https://en.wikipedia.org/wiki/Marshalling_%28computer_science%29
29

310
### Basic Usage
411

@@ -23,7 +30,18 @@ enum Sass_Tag {
2330
// Tags for denoting Sass list separators
2431
enum Sass_Separator {
2532
SASS_COMMA,
26-
SASS_SPACE
33+
SASS_SPACE,
34+
// only used internally to represent a hash map before evaluation
35+
// otherwise we would be too early to check for duplicate keys
36+
SASS_HASH
37+
};
38+
39+
// Value Operators
40+
enum Sass_OP {
41+
AND, OR, // logical connectives
42+
EQ, NEQ, GT, GTE, LT, LTE, // arithmetic relations
43+
ADD, SUB, MUL, DIV, MOD, // arithmetic functions
44+
NUM_OPS // so we know how big to make the op table
2745
};
2846
```
2947

@@ -33,6 +51,32 @@ enum Sass_Separator {
3351
// Forward declaration
3452
union Sass_Value;
3553

54+
// Creator functions for all value types
55+
union Sass_Value* sass_make_null (void);
56+
union Sass_Value* sass_make_boolean (bool val);
57+
union Sass_Value* sass_make_string (const char* val);
58+
union Sass_Value* sass_make_qstring (const char* val);
59+
union Sass_Value* sass_make_number (double val, const char* unit);
60+
union Sass_Value* sass_make_color (double r, double g, double b, double a);
61+
union Sass_Value* sass_make_list (size_t len, enum Sass_Separator sep);
62+
union Sass_Value* sass_make_map (size_t len);
63+
union Sass_Value* sass_make_error (const char* msg);
64+
union Sass_Value* sass_make_warning (const char* msg);
65+
66+
// Generic destructor function for all types
67+
// Will release memory of all associated Sass_Values
68+
// Means we will delete recursively for lists and maps
69+
void sass_delete_value (union Sass_Value* val);
70+
71+
// Make a deep cloned copy of the given sass value
72+
union Sass_Value* sass_clone_value (const union Sass_Value* val);
73+
74+
// Stringify a Sass_Values and also return the result as a Sass_Value (of type STRING)
75+
union Sass_Value* sass_value_stringify (const union Sass_Value* a, bool compressed, int precision);
76+
77+
// Execute an operation for two Sass_Values and return the result as a Sass_Value too
78+
union Sass_Value* sass_value_op (enum Sass_OP op, const union Sass_Value* a, const union Sass_Value* b);
79+
3680
// Return the sass tag for a generic sass value
3781
// Check is needed before accessing specific values!
3882
enum Sass_Tag sass_value_get_tag (const union Sass_Value* v);
@@ -58,6 +102,8 @@ void sass_number_set_unit (union Sass_Value* v, char* unit);
58102
// Getters and setters for Sass_String
59103
const char* sass_string_get_value (const union Sass_Value* v);
60104
void sass_string_set_value (union Sass_Value* v, char* value);
105+
bool sass_string_is_quoted(const union Sass_Value* v);
106+
void sass_string_set_quoted(union Sass_Value* v, bool quoted);
61107

62108
// Getters and setters for Sass_Boolean
63109
bool sass_boolean_get_value (const union Sass_Value* v);
@@ -84,7 +130,7 @@ void sass_list_set_value (union Sass_Value* v, size_t i, union Sass_Value* value
84130

85131
// Getter for the number of items in map
86132
size_t sass_map_get_length (const union Sass_Value* v);
87-
// Getters and setters for Sass_List keys and values
133+
// Getters and setters for Sass_Map keys and values
88134
union Sass_Value* sass_map_get_key (const union Sass_Value* v, size_t i);
89135
void sass_map_set_key (union Sass_Value* v, size_t i, union Sass_Value*);
90136
union Sass_Value* sass_map_get_value (const union Sass_Value* v, size_t i);
@@ -97,25 +143,6 @@ void sass_error_set_message (union Sass_Value* v, char* msg);
97143
// Getters and setters for Sass_Warning
98144
char* sass_warning_get_message (const union Sass_Value* v);
99145
void sass_warning_set_message (union Sass_Value* v, char* msg);
100-
101-
// Creator functions for all value types
102-
union Sass_Value* sass_make_null (void);
103-
union Sass_Value* sass_make_boolean (bool val);
104-
union Sass_Value* sass_make_string (const char* val);
105-
union Sass_Value* sass_make_number (double val, const char* unit);
106-
union Sass_Value* sass_make_color (double r, double g, double b, double a);
107-
union Sass_Value* sass_make_list (size_t len, enum Sass_Separator sep);
108-
union Sass_Value* sass_make_map (size_t len);
109-
union Sass_Value* sass_make_error (const char* msg);
110-
union Sass_Value* sass_make_warning (const char* msg);
111-
112-
// Generic destructor function for all types
113-
// Will release memory of all associated Sass_Values
114-
// Means we will delete recursively for lists and maps
115-
void sass_delete_value (union Sass_Value* val);
116-
117-
// Make a deep cloned copy of the given sass value
118-
union Sass_Value* sass_clone_value (const union Sass_Value* val);
119146
```
120147
121148
### More links

include/sass/base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ ADDAPI char* ADDCALL sass_string_unquote (const char* str);
7676
// Resolve a file via the given include paths in the include char* array
7777
ADDAPI char* ADDCALL sass_resolve_file (const char* path, const char* incs[]);
7878

79-
// Get compiled libsass version
79+
// Implemented sass language version
80+
// Hardcoded version 3.4 for time being
8081
ADDAPI const char* ADDCALL libsass_version(void);
8182

8283
// Get compiled libsass language

include/sass/values.h

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ enum Sass_Tag {
3030
enum Sass_Separator {
3131
SASS_COMMA,
3232
SASS_SPACE,
33+
// only used internally to represent a hash map before evaluation
34+
// otherwise we would be too early to check for duplicate keys
3335
SASS_HASH
3436
};
3537

@@ -41,6 +43,32 @@ enum Sass_OP {
4143
NUM_OPS // so we know how big to make the op table
4244
};
4345

46+
// Creator functions for all value types
47+
ADDAPI union Sass_Value* ADDCALL sass_make_null (void);
48+
ADDAPI union Sass_Value* ADDCALL sass_make_boolean (bool val);
49+
ADDAPI union Sass_Value* ADDCALL sass_make_string (const char* val);
50+
ADDAPI union Sass_Value* ADDCALL sass_make_qstring (const char* val);
51+
ADDAPI union Sass_Value* ADDCALL sass_make_number (double val, const char* unit);
52+
ADDAPI union Sass_Value* ADDCALL sass_make_color (double r, double g, double b, double a);
53+
ADDAPI union Sass_Value* ADDCALL sass_make_list (size_t len, enum Sass_Separator sep);
54+
ADDAPI union Sass_Value* ADDCALL sass_make_map (size_t len);
55+
ADDAPI union Sass_Value* ADDCALL sass_make_error (const char* msg);
56+
ADDAPI union Sass_Value* ADDCALL sass_make_warning (const char* msg);
57+
58+
// Generic destructor function for all types
59+
// Will release memory of all associated Sass_Values
60+
// Means we will delete recursively for lists and maps
61+
ADDAPI void ADDCALL sass_delete_value (union Sass_Value* val);
62+
63+
// Make a deep cloned copy of the given sass value
64+
ADDAPI union Sass_Value* ADDCALL sass_clone_value (const union Sass_Value* val);
65+
66+
// Execute an operation for two Sass_Values and return the result as a Sass_Value too
67+
ADDAPI union Sass_Value* ADDCALL sass_value_op (enum Sass_OP op, const union Sass_Value* a, const union Sass_Value* b);
68+
69+
// Stringify a Sass_Values and also return the result as a Sass_Value (of type STRING)
70+
ADDAPI union Sass_Value* ADDCALL sass_value_stringify (const union Sass_Value* a, bool compressed, int precision);
71+
4472
// Return the sass tag for a generic sass value
4573
// Check is needed before accessing specific values!
4674
ADDAPI enum Sass_Tag ADDCALL sass_value_get_tag (const union Sass_Value* v);
@@ -108,33 +136,6 @@ ADDAPI void ADDCALL sass_error_set_message (union Sass_Value* v, char* msg);
108136
ADDAPI char* ADDCALL sass_warning_get_message (const union Sass_Value* v);
109137
ADDAPI void ADDCALL sass_warning_set_message (union Sass_Value* v, char* msg);
110138

111-
// Creator functions for all value types
112-
ADDAPI union Sass_Value* ADDCALL sass_make_null (void);
113-
ADDAPI union Sass_Value* ADDCALL sass_make_boolean (bool val);
114-
ADDAPI union Sass_Value* ADDCALL sass_make_string (const char* val);
115-
ADDAPI union Sass_Value* ADDCALL sass_make_qstring (const char* val);
116-
ADDAPI union Sass_Value* ADDCALL sass_make_number (double val, const char* unit);
117-
ADDAPI union Sass_Value* ADDCALL sass_make_color (double r, double g, double b, double a);
118-
ADDAPI union Sass_Value* ADDCALL sass_make_list (size_t len, enum Sass_Separator sep);
119-
ADDAPI union Sass_Value* ADDCALL sass_make_map (size_t len);
120-
ADDAPI union Sass_Value* ADDCALL sass_make_error (const char* msg);
121-
ADDAPI union Sass_Value* ADDCALL sass_make_warning (const char* msg);
122-
123-
// Generic destructor function for all types
124-
// Will release memory of all associated Sass_Values
125-
// Means we will delete recursively for lists and maps
126-
ADDAPI void ADDCALL sass_delete_value (union Sass_Value* val);
127-
128-
// Make a deep cloned copy of the given sass value
129-
ADDAPI union Sass_Value* ADDCALL sass_clone_value (const union Sass_Value* val);
130-
131-
// Stringify a Sass_Values and also return the result as a Sass_Value (of type STRING)
132-
ADDAPI union Sass_Value* ADDCALL sass_value_stringify (const union Sass_Value* a, bool compressed, int precision);
133-
134-
// Execute an operation for two Sass_Values and return the result as a Sass_Value too
135-
ADDAPI union Sass_Value* ADDCALL sass_value_op (enum Sass_OP op, const union Sass_Value* a, const union Sass_Value* b);
136-
137-
138139
#ifdef __cplusplus
139140
} // __cplusplus defined.
140141
#endif

0 commit comments

Comments
 (0)