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 = "/* \#{structure[\"s2name\"]} */  \n  HEAD\n\n\n  create_definition = <<-CREATE_DEFINITION\n\n{\n\#{make_null_check(\"out_object\", \"S2_OBJECT_IS_NULL\")}\n  *out_object = calloc(1, sizeof(\#{structure_name}));\n  return S2_NO_ERROR;\n}\n  CREATE_DEFINITION\n\n  delete_definition = <<-DELETE_DEFINITION\n\n{\n\#{make_null_check(\"in_object\", \"S2_OBJECT_IS_NULL\")}\n  free(in_object);\n  return S2_NO_ERROR;\n}\n  DELETE_DEFINITION\n\n  if !is_source\n    create_definition = \";\"\n    delete_definition = \";\"\n  end\n\n  function_defines += <<-PROTOTYPES\nS2Error Create\#{structure_name}(\#{structure_name}** out_object)\#{create_definition}\nS2Error Delete\#{structure_name}(\#{structure_name}* in_object)\#{delete_definition}\n  PROTOTYPES\n\n  structure[\"fields\"].each do |field|\n    field_name = field[\"name\"]\n    field_type = c_type(field[\"type\"])\n\n    get_definition = <<-GET_DEFINITION\n\n{\n\#{make_null_check(\"in_object\", \"S2_OBJECT_IS_NULL\")}\n\#{make_null_check(\"out_value\", \"S2_VALUE_IS_NULL\")}\n  *out_value = in_object->\#{field_name};\n  return S2_NO_ERROR;\n}\n    GET_DEFINITION\n\n    set_definition = <<-SET_DEFINITION\n\n{\n\#{make_null_check(\"in_object\", \"S2_OBJECT_IS_NULL\")}\n  in_object->\#{field_name} = in_value;\n  return S2_NO_ERROR;\n}\n    SET_DEFINITION\n\n    if !is_source\n      get_definition = \";\"\n      set_definition = \";\"\n    end\n\n    function_defines += <<-PROTOTYPES\nS2Error Get\#{structure_name}_\#{field_name}(\#{structure_name}* in_object, \#{field_type}* out_value)\#{get_definition}\nS2Error Set\#{structure_name}_\#{field_name}(\#{structure_name}* in_object, \#{field_type} in_value)\#{set_definition}\n    PROTOTYPES\n    \n\n  end\n\n  function_defines\nend\n"

#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)
"  if (\#{target} == NULL)\n  {\n    return \#{error};\n  }\n"
end