Browse Source

ig-c99: extract lm-c99-api and refactoring

main
heck 2 years ago
parent
commit
c657a2fc3c
  1. 287
      lm-c99/ig-c99.ysl2

287
lm-c99/ig-c99.ysl2

@ -8,88 +8,17 @@ tstylesheet {
include standardlib.ysl2
include idl_api.ysl2
// ----------------------------------------------------------------------
// pEpACIDLang IG-C99
// The C99 Interface Generator for pEpACIDLang
// pEpACIDLang LM-C99-API
// The C99 Language mapping API
// ----------------------------------------------------------------------
// generate basetype declarations defined by the idl
template "idl" document "ig-c99/idl_core.h", "text" {
||
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// idl basetypes
// -------------
``apply "basetypes", 0
#ifdef __cplusplus
} // extern "C"
#endif
||
}
template "basetypes" {
for "*" {
const "name", "name()";
if "func:c99_typename_of_basetype($name) != $name" {
> «func:typedef(func:c99_typename_of_basetype($name), $name)»
}
}
}
template "api" document "ig-c99/{@name}_api.h", "text" {
const "pkg_name", "../@name";
const "pkg_version", "../version/text()";
const "pkg_license", "../license/text()";
const "pkg_copyleft", "../copyleft/text()";
const "api_name", "@name";
||
// «yml:ucase($pkg_name)» «$pkg_version»
// «$api_name»_api.h
// «$pkg_license»
// «$pkg_copyleft»
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "idl_core.h"
// deftypes
// --------
``apply "deftype", 0
``call "enums"
// Structs
// -------
``apply "struct", 0
// Functions
// ---------
``apply "method", 0
#ifdef __cplusplus
} // extern "C"
#endif
||
}
def "func:c99_typename_of_basetype" {
param "idltype";
// TODO: test for is_basetype
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$idltype = 'string'"
result > char*
@ -108,33 +37,50 @@ tstylesheet {
otherwise
error > func:c99_typename_of_basetype - basetype not supported by ig-c99: «$idltype»
}
otherwise
error > func:c99_typename_of_basetype - idltype: «$idltype» is not defined
}
}
def "func:c99_typename_of_enum" {
param "name";
param "pkg_name";
result "yml:ucase(concat($pkg_name, '_', $name))";
param "idl_enum_name";
result "yml:ucase(concat(func:idl_get_package_name(), '_', $idl_enum_name))";
}
def "func:c99_itemname_of_enumitem" {
param "enum_name";
param "idl_item_name";
result "yml:ucase(concat($enum_name, '_', $idl_item_name))";
def "func:c99_typename_of_struct" {
param "idl_struct_name";
result > «yml:capit(func:idl_get_package_name())»_«yml:capit($idl_struct_name)»;
}
def "func:c99_typename_of_struct" {
param "pkg_name";
param "idl_name";
def "func:c99_typename_of_idltype" {
param "idltype";
const "idl_typekind", "func:idl_type_get_typekind_of_type($idltype)";
result "concat(yml:capit($pkg_name), '_', yml:capit($idl_name))";
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$idl_typekind = 'basetype'"
result "func:c99_typename_of_basetype($idltype)";
when "$idl_typekind = 'deftype'"
result "$idltype";
when "$idl_typekind = 'enum'"
result "func:c99_typename_of_enum($idltype)";
when "$idl_typekind = 'struct'"
result "func:c99_typename_of_struct($idltype)";
when "$idl_typekind = 'generic'"
error > c99_typename_of_type - idltype «$idltype» is a generic type and can only be used in deftype declarations.
otherwise
error > c99_typename_of_type - cant determine c99 typename for idltype: «$idltype»
}
otherwise
error > c99_typename_of_idltype - is not an idltype: «$idltype»
}
}
// typeclass is either
// * primitive
// * object
def "func:c99_typeclass_of_type" {
def "func:c99_typeclass_of_idltype" {
param "idltype";
const "resolved_type", "func:idl_type_get_resolved_type($idltype)";
const "resolved_typekind", "func:idl_type_get_typekind_of_type($resolved_type)";
@ -158,38 +104,126 @@ tstylesheet {
when "$resolved_typekind = 'generic'"
result > object
otherwise
error > c99_typeclass_of_type - cant determine typeclass of: «$idltype»
error > c99_typeclass_of_idltype - cant determine typeclass of: «$idltype»
}
otherwise
error > c99_typeclass_of_type - is not a type: «$idltype»
error > c99_typeclass_of_idltype - is not an idltype: «$idltype»
}
};
// TYPEALIAS
// ---------
def "func:c99_itemname_of_enumitem" {
param "idl_enum_name";
param "idl_item_name";
result "yml:ucase(concat($idl_enum_name, '_', $idl_item_name))";
}
// ----------------------------------------------------------------------
// pEpACIDLang IG-C99
// The C99 Interface Generator for pEpACIDLang
// ----------------------------------------------------------------------
// generate basetype declarations defined by the idl
template "idl" document "ig-c99/idl_core.h", "text" {
||
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// idl basetypes
// -------------
``apply "basetypes", 0
#ifdef __cplusplus
} // extern "C"
#endif
||
}
template "basetypes" {
for "*" {
const "name", "name()";
if "func:c99_typename_of_idltype($name) != $name" {
| «func:typedef(func:c99_typename_of_idltype($name), $name)»
}
}
}
template "api" document "ig-c99/{@name}_api.h", "text" {
const "api_name", "@name";
||
// «yml:ucase(func:idl_get_package_name())» version «func:idl_get_package_version()»
// «$api_name»_api.h
// «func:idl_get_package_license()»
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "idl_core.h"
``call "deftypes"
``call "enums"
``call "structs"
``call "methods"
#ifdef __cplusplus
} // extern "C"
#endif
||
}
// DEFTYPE
// -------
function "deftypes" {
||
// Deftypes
// --------
``apply "deftype"
||
}
template "deftype" {
const "alias_name", "@name";
const "type_or_typekind", "as/@type";
call "deftype"
with "new_typename", "@name",
with "aliased_type_or_typekind", "as/@type";
}
function "deftype" {
param "new_typename";
param "aliased_type_or_typekind";
choose {
when 'func:idl_typekind_is_generic($type_or_typekind)'
when 'func:idl_typekind_is_generic($aliased_type_or_typekind)'
choose {
when "$type_or_typekind = 'list'"
> «func:typedef($type_or_typekind, $alias_name)»
when "$aliased_type_or_typekind = 'list'"
| «func:typedef($aliased_type_or_typekind, $new_typename)»
otherwise
error > deftype - typekind must be generic «$type_or_typeind»
}
otherwise
> «func:typedef($type_or_typekind, $alias_name)»
| «func:typedef($aliased_type_or_typekind, $new_typename)»
}
}
def "func:typedef" {
param "is_type";
param "alias_name";
result > typedef «$is_type» «$alias_name»;\n
param "new_typename";
result > typedef «$is_type» «$new_typename»;
}
@ -203,19 +237,17 @@ tstylesheet {
// -----
``apply "enum"
||
}
template "enum" {
call "enum"
with "idl_enum_name", "@name",
with "idl_pkg_name", "../../@name";
call "enum" with "idl_enum_name", "@name";
}
function "enum" {
param "idl_enum_name";
param "idl_pkg_name";
const "enum_name", "func:c99_typename_of_enum($idl_enum_name, $idl_pkg_name)";
const "enum_name", "func:c99_typename_of_idltype($idl_enum_name)";
||
typedef enum _«$enum_name» {
``apply "item" with "enum_name", "$enum_name"
@ -229,13 +261,13 @@ tstylesheet {
const "idl_item_name", "@name";
const "c99_item_name", "func:c99_itemname_of_enumitem($enum_name, $idl_item_name)";
const "idl_value", "text()";
const "c99_value", "func:enum_item_value($idl_value)";
const "c99_value", "func:c99_enum_item_value($idl_value)";
apply "@doc", 0, mode="multiline";
| «$c99_item_name»«$c99_value»«func:autocomma()»`apply "@doc" , 0, mode="oneline"`
}
def "func:enum_item_value" {
def "func:c99_enum_item_value" {
param "idl_value";
choose {
when "string-length($idl_value) > 0"
@ -243,7 +275,6 @@ tstylesheet {
otherwise
result >
}
}
// STRUCT
@ -255,17 +286,22 @@ tstylesheet {
// -------
``apply "struct"
||
}
template "struct" {
const "idl_name", "@name";
const "pkg_name", "../../@name";
const "c_name", "func:c99_typename_of_struct($pkg_name, $idl_name)";
call "struct" with "idl_struct_name", "@name";
}
function "struct" {
param "idl_struct_name";
const "c99_struct_name", "func:c99_typename_of_idltype($idl_struct_name)";
||
typedef struct _«$c_name» {
typedef struct _«$c99_struct_name» {
``apply "field"
} «$c_name»;
} «$c99_struct_name»;
||
}
@ -282,10 +318,25 @@ tstylesheet {
// METHOD
// ------
function "methods" {
||
// Functions
// ---------
``apply "method"
||
}
template "method" {
const "name", "@name";
call "method"
with "idl_method_name", "@name";
}
function "method" {
param "idl_method_name";
||
PEP_STATUS «$name»(
PEP_STATUS «$idl_method_name»(
``apply "param", 0
);
@ -302,7 +353,7 @@ tstylesheet {
def "func:idlparam_to_cparam" {
param "idltype";
param "mode";
const "typeclass", "func:c99_typeclass_of_type($idltype)";
const "typeclass", "func:c99_typeclass_of_idltype($idltype)";
choose {
when "$mode = 'borrow'"

Loading…
Cancel
Save