Top Level Namespace

Includes:
S2_parse

Defined Under Namespace

Modules: S2, S2_parse

Instance Method Summary collapse

Methods included from S2_parse

compile_to_ast, runtest

Instance Method Details

#c_type(typename) ⇒ Object



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
# File 'lib/s2/internal/c.rb', line 2

def c_type(typename)

	case typename
	when "Int8"
		"int8_t"
	when "Int16"
		"int16_t"
	when "Int32"
		"int32_t"
	when "Int64"
		"int64_t"
	when "Uint8"
		"uint8_t"
	when "Uint16"
		"uint16_t"
	when "Uint32"
		"uint32_t"
	when "Uint64"
		"uint64_t"
	when "Float32"
		"float"
	when "Float64"
		"double"
	else
		"#{typename}*"
	end
end

#error_typesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/s2/internal/c.rb', line 30

def error_types
	[
		"S2_NO_ERROR",
		"S2_OUT_OF_MEMORY",
		"S2_INDEX_OUT_OF_BOUNDS",
		"S2_UNEXPECTED_END_OF_FILE",
		"S2_OBJECT_IS_NULL",
		"S2_VALUE_IS_NULL",
		"S2_WRITE_FAILED",
		"S2_POINTER_IS_WRONG_TYPE",
		"S2_NULL_FILE_POINTER",
		"S2_MEMBER_VALUE_WRONG",
		"S2_INVALID_VALUE_FOR_TYPE",
		"S2_END_OF_STACK"
	]
end

#make_function_defines(structure_name, structure, is_source:) ⇒ Object



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
125
126
127
128
# File 'lib/s2/internal/c.rb', line 56

def make_function_defines(structure_name, structure, is_source:)

	function_defines = <<-HEAD
/* #{structure["s2name"]} */	
	HEAD


	create_definition = <<-CREATE_DEFINITION

{
#{make_null_check("out_object", "S2_OBJECT_IS_NULL")}
	*out_object = calloc(1, sizeof(#{structure_name}));
	return S2_NO_ERROR;
}
	CREATE_DEFINITION

	delete_definition = <<-DELETE_DEFINITION

{
#{make_null_check("in_object", "S2_OBJECT_IS_NULL")}
	free(in_object);
	return S2_NO_ERROR;
}
	DELETE_DEFINITION

	if !is_source
		create_definition = ";"
		delete_definition = ";"
	end

	function_defines += <<-PROTOTYPES
S2Error Create#{structure_name}(#{structure_name}** out_object)#{create_definition}
S2Error Delete#{structure_name}(#{structure_name}* in_object)#{delete_definition}
	PROTOTYPES

	structure["fields"].each do |field|
		field_name = field["name"]
		field_type = c_type(field["type"])

		get_definition = <<-GET_DEFINITION

{
#{make_null_check("in_object", "S2_OBJECT_IS_NULL")}
#{make_null_check("out_value", "S2_VALUE_IS_NULL")}
	*out_value = in_object->#{field_name};
	return S2_NO_ERROR;
}
		GET_DEFINITION

		set_definition = <<-SET_DEFINITION

{
#{make_null_check("in_object", "S2_OBJECT_IS_NULL")}
	in_object->#{field_name} = in_value;
	return S2_NO_ERROR;
}
		SET_DEFINITION

		if !is_source
			get_definition = ";"
			set_definition = ";"
		end

		function_defines += <<-PROTOTYPES
S2Error Get#{structure_name}_#{field_name}(#{structure_name}* in_object, #{field_type}* out_value)#{get_definition}
S2Error Set#{structure_name}_#{field_name}(#{structure_name}* in_object, #{field_type} in_value)#{set_definition}
		PROTOTYPES
		

	end

	function_defines
end

#make_null_check(target, error) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/s2/internal/c.rb', line 47

def make_null_check(target, error)
<<-CHECK
	if (#{target} == NULL)
	{
		return #{error};
	}
CHECK
end