Bash++
Bash++ compiler internal documentation
templates.h
Go to the documentation of this file.
1 
6 #ifndef SRC_BPP_INCLUDE_TEMPLATES_H_
7 #define SRC_BPP_INCLUDE_TEMPLATES_H_
8 
9 static const char* bpp_supershell_function = R"EOF(function bpp____initsupershell() {
10  local bpp____supershellDirectory="/dev/shm/"
11  if [[ ! -d "${bpp____supershellDirectory}" ]]; then
12  bpp____supershellDirectory="${TMPDIR:-/tmp/}"
13  fi
14  local bpp____supershelltempfile="$(mktemp "${bpp____supershellDirectory}/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")"
15  eval "exec {bpp____supershellFD__$BASHPID}<>\"$bpp____supershelltempfile\""
16  rm "$bpp____supershelltempfile"
17 }
18 function bpp____supershell() {
19  local __outputVar="$1" __command="$2" __supershellFD="bpp____supershellFD__$BASHPID" __temporaryStorage=""
20  if [[ -z "${!__supershellFD}" ]]; then
21  bpp____initsupershell
22  else
23  __temporaryStorage=$(< "/dev/fd/${!__supershellFD}")
24  fi
25  $__command 1>"/dev/fd/${!__supershellFD}" 2>/dev/null
26  eval "$__outputVar=\$(< "/dev/fd/${!__supershellFD}")"
27  echo "${__temporaryStorage}">"/dev/fd/${!__supershellFD}"
28 }
29 )EOF";
30 
31 static const char* bpp_vtable_lookup = R"EOF(function bpp____vTable__lookup() {
32  local __objectAddress="$1" __method="$2" __outputVar="$3"
33  ([[ -z "${__objectAddress}" ]] || [[ -z "${__method}" ]] || [[ -z "${__outputVar}" ]]) && >&2 echo "Bash++: Error: Invalid vTable lookup" && exit 1
34  while : ; do
35  if ! eval "declare -p \"${__objectAddress}\"" &>/dev/null; then
36  break
37  fi
38  [[ -z "${!__objectAddress}" ]] && break
39  __objectAddress="${!__objectAddress}"
40  done
41  local __vTable="${__objectAddress}____vPointer"
42  if ! eval "declare -p \"${__vTable}\"" &>/dev/null; then
43  return 1
44  fi
45  local __result="${!__vTable}[\"${__method}\"]"
46  [[ -z "${!__result}" ]] && >&2 echo "Bash++: Error: Method '${__method}' not found in vTable for object '${__objectAddress}'" && return 1
47  eval "${__outputVar}=\$__result"
48 }
49 )EOF";
50 
51 static const char* bpp_dynamic_cast = R"EOF(function bpp____dynamic__cast() {
52  local __objectAddress="$1" __type="$2" __outputVar="$3"
53  ([[ -z "${__objectAddress}" ]] || [[ -z "${__type}" ]] || [[ -z "${__outputVar}" ]]) && >&2 echo "Bash++: Error: Invalid dynamic_cast" && exit 1
54  eval "${__outputVar}=0"
55  while [[ ! -z "${!__objectAddress}" ]] 2>/dev/null; do
56  __objectAddress="${!__objectAddress}"
57  done
58  local __vTable="${__objectAddress}____vPointer"
59  while [[ ! -z "${!__vTable}" ]] 2>/dev/null; do
60  [[ "${!__vTable}" == "bpp__${__type}____vTable" ]] && eval "${__outputVar}=\"${__objectAddress}\"" && return 0
61  __vTable="${!__vTable}[\"__parent__\"]"
62  done
63  return 1
64 }
65 )EOF";
66 
67 static const char* template_new_function = R"EOF(function bpp__%CLASS%____new() {
68  local __objectAddress="$1"
69  if [[ "${__objectAddress}" == "" ]]; then
70  while : ; do
71  __objectAddress="bpp__%CLASS%__$RANDOM$RANDOM$RANDOM$RANDOM"
72  local __unusedVar="${__objectAddress}____vPointer"
73  [[ -z "${!__unusedVar+x}" ]] && break
74  done
75  fi
76  eval "${__objectAddress}____vPointer=bpp__%CLASS%____vTable"
77 %ASSIGNMENTS%
78  echo "${__objectAddress}"
79 }
80 )EOF";
81 
82 static const char* template_copy_function = R"EOF(function bpp__%CLASS%____copy() {
83  local __copyFromAddress="$1" __copyToAddress="$2"
84  while : ; do
85  if ! eval "declare -p \"${__copyFromAddress}\"" &>/dev/null; then
86  break
87  fi
88  [[ -z "${!__copyFromAddress}" ]] && break
89  __copyFromAddress="${!__copyFromAddress}"
90  done
91  while : ; do
92  if ! eval "declare -p \"${__copyToAddress}\"" &>/dev/null; then
93  break
94  fi
95  [[ -z "${!__copyToAddress}" ]] && break
96  __copyToAddress="${!__copyToAddress}"
97  done
98  local __copyFromVPointer="${__copyFromAddress}____vPointer" __copyToVPointer="${__copyToAddress}____vPointer"
99  if [[ "${__copyFromAddress}" == "0" ]] || [[ -z "${!__copyFromVPointer}" ]]; then
100  >&2 echo "Bash++: Error: %CLASS%: Attempted to copy from null object"
101  return
102  fi
103  if [[ "${__copyToAddress}" == "0" ]] || [[ -z "${!__copyToVPointer}" ]]; then
104  >&2 echo "Bash++: Error: %CLASS%: Attempted to copy to null object"
105  return
106  fi
107 %COPIES%
108  eval "${__copyToVPointer}=${!__copyFromVPointer}"
109 }
110 )EOF";
111 
112 static const char* template_method = R"EOF(function bpp__%CLASS%__%SIGNATURE%() {
113  local __objectAddress="$1"
114  shift 1
115  %PARAMS%
116  while : ; do
117  if ! eval "declare -p \"${__objectAddress}\"" &>/dev/null; then
118  break
119  fi
120  [[ -z "${!__objectAddress}" ]] && break
121  __objectAddress="${!__objectAddress}"
122  done
123  local __vPointer="${__objectAddress}____vPointer"
124  if [[ "${__objectAddress}" == "0" ]] || [[ -z "${!__vPointer}" ]]; then
125  >&2 echo "Bash++: Error: Attempted to call @%CLASS%.%SIGNATURE% on null object"
126  return
127  fi
128 %METHODBODY%
129 }
130 )EOF";
131 
132 #endif // SRC_BPP_INCLUDE_TEMPLATES_H_
static const char * bpp_vtable_lookup
Definition: templates.h:31
static const char * bpp_dynamic_cast
Definition: templates.h:51
static const char * template_new_function
Definition: templates.h:67
static const char * bpp_supershell_function
Definition: templates.h:9
static const char * template_copy_function
Definition: templates.h:82
static const char * template_method
Definition: templates.h:112