wget https://github.com/mrubyc/mrubyc/archive/refs/tags/release3.4.1.zip
unzip release3.4.1.zip
cd mrubyc-release3.4.1/src
cp ../hal/minimal.h hal.h
# setting
sed -i -e 's@#endif@#ifdef __cplusplus\n}\n#endif\n#endif@' -e 's@#define hal_init@#ifdef __cplusplus\nextern "C" {\n#endif\n#define hal_init@' hal.h
sed -i -e 's/#define MRBC_USE_FLOAT 2/#if defined(CH32V20x)\n#define MRBC_USE_FLOAT 0\n#else\n#define MRBC_USE_FLOAT 2\n#endif/' -e 's@// #define MRBC_REQUIRE_32BIT_ALIGNMENT@#if defined(ARDUINO_ARCH_ESP8266) || defined(CH32V20x)\n#define MRBC_REQUIRE_32BIT_ALIGNMENT\n#endif@' vm_config.h
sed -i -e 's@#include "mrubyc.h"@#include "mrubyc.h"\n#include "rrt0.h"@' rrt0.c
# for float
awk '/->d/{$0="#if MRBC_USE_FLOAT\n"$0"\n#endif"}1' boxing_no.h >temp.tmp && mv temp.tmp boxing_no.h
awk -v RS="" '/MRBC_TT_FLOAT/ && !/MRBC_USE_FLOAT/{$0="#if MRBC_USE_FLOAT\n"$0"\n#endif"}/return val->d;\n\}/{$0="#if MRBC_USE_FLOAT\n  return val->d;\n#else\n  return 0;\n#endif\n}"}{$0=$0"\n"}1' value.c >temp.tmp && mv temp.tmp value.c
awk '/MRBC_CLASS\(Float/{$0="#if MRBC_USE_FLOAT\n"$0"\n#else\n  0,\n#endif"}1' class.c >temp.tmp && mv temp.tmp class.c
awk '/define MRBC_TO_INT/{$0="#if MRBC_USE_FLOAT\n"$0;a=NR}a&&NR==a+3{$0="#else\n#define MRBC_TO_INT(val) (val).i\n#endif\n"$0}1' value.h >temp.tmp && mv temp.tmp value.h
# for string
awk '/MRBC_CLASS\(String/{$0="#if MRBC_USE_STRING\n"$0"\n#else\n  0,\n#endif"}1' class.c >temp.tmp && mv temp.tmp class.c
cat c_object.c |awk '/void mrbc_object_inspect/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/static int set_sym_name_by_id/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' >temp.tmp && mv temp.tmp c_object.c
cat c_object.h |awk '/void mrbc_object_inspect/{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp c_object.h
cat error.c |awk '/static void c_exception_message/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/TT_STRING/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/}/{$0=$0"\n#endif";a=0;b=1}b&&/}$/{$0="#if MRBC_USE_STRING\n"$0"\n#endif";b=0}1' |awk '/c_exception_message /{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp error.c
cat rrt0.c |awk '/in case of Task.get\("TasName"\)/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/}/{$0=$0"\n#endif";a=0}1' |awk '/void c_task_name/||/void c_task_set_name/||/void c_task_status/||/void c_task_create/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/c_task_name_list /||/c_task_name /||/c_task_set_name /||/c_task_status /||/c_task_create /{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp rrt0.c
# printf int
cat console.c | awk "/case 'B'/{\$0=\$0\"\n  case 'o':\";print;r=4}r{r-=1;next}1" |sed 's/, 4)/, pf->fmt.type*9\/2%8)/' |awk 'a==0 && /int pad_width/{$0=$0"\n    if(pf->fmt.flag_zero && pf->fmt.precision<pad_width) pf->fmt.precision=pad_width;";a=1}1' |ruby -ple "sub(/\"0123456789ABCDEF\"\\[(.+)\\]/){\"((#\$1) < 10) ? (#\$1)+'0' : (#\$1)+'A'-10\"}" |awk "/buf\\[0\\] = '\\\\\\\\';\$/{\$0=\"//\"\$0}/void mrbc_char_to_s/{a=1}a&&/^{/{\$0=\$0\"\n  buf[0]='\\\\\\\\';\";a=0}1" >temp.tmp && mv temp.tmp console.c
sed -i -e 's/snprintf/mrbc_snprintf/' value.c
sed -i -e 's/int mrbc_printf_float/int mrbc_printf_float2/' -e 's/pf->fmt.precision &&/pf->fmt.precision > 0 \&\&/' -e 's/pf->fmt.precision != 0/pf->fmt.precision > 0/' -e 's/(struct RPrintfFormat){0};/(struct RPrintfFormat){0};\n  pf->fmt.precision=-1;/' -e "s/fstr == '\.' ) {/fstr == '.' ) {\n    pf->fmt.precision=0;/" console.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/floatformat.c >>console.c
# reduce size
cat c_object.c | awk "/case 'B'/{print;b=6}b{b-=1;next}/case 'X'/{print;x=6}x{x-=1;next}1" |sed 's/, 3)/, pf.fmt.type*9\/2%8)/' >temp.tmp && mv temp.tmp c_object.c
cat c_string.c | awk '/int is_space/{a=b=1}a && /^{/{$0="/*"$0;a=0}b && /^}/{$0=$0"*/\n{ return (8 < ch && ch < 14); }";b=0}1' >temp.tmp &&  mv temp.tmp c_string.c
cat alloc.c | awk '/int nlz16/{$0="/*"$0;b=1}b && /^}/{$0=$0"*/";b=0}/int nlz8/{c=1}c && /^}/{$0=$0"\nstatic inline int nlz16(uint16_t x)\n{ return ((x >> 8) == 0 )? 8+nlz8(x&0xff): nlz8(x>>8); }";c=0}1' >temp.tmp && mv temp.tmp alloc.c
# simple functions
sed -i -e "s@strpbrk(buf, \".e\")@strchr(buf, \'.\') || strchr(buf, \'e\')@" value.c
sed -i -e 's/atol(mrbc_string_cstr(&v\[i\]));/mrbc_atoi( mrbc_string_cstr(\&v[i]),10 );/' c_object.c
sed -i -e 's/atof/mrbc_atof/' c_string.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/atof.c >> value.c
awk '/atoi/{$0=$0"\nmrbc_float_t mrbc_atof(const char *s);"}1' value.h >temp.tmp && mv temp.tmp value.h
#
#sed -i -e 's@extern@#if defined(CH32V20x)\n  extern@' -e 's@(mrblib_bytecode);@(mrblib_bytecode);\n  #endif@' class.c
sed -i -e 's@#include "rrt0.h"@#if !defined(CH32V20x)\n#include "rrt0.h"\n#endif\n#include "mrbc_get.h"\n#include "arduino_gpio.h"\n#include "arduino_adc.h"\n#include "arduino_pwm.h"\n#include "arduino_i2c.h"\n#include "arduino_spi.h"\n#include "arduino_uart.h"\n#include "arduino_wifi.h"@' mrubyc.h
# arduino files
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_gpio.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_gpio.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_pwm.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_pwm.cpp
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_adc.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_adc.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_i2c.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_i2c.cpp
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_spi.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_spi.cpp
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_uart.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_uart.cpp
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_wifi.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arduino_wifi.cpp
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/mrbc_get.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/mrbc_get.c
# make_output_buffer
wget -O - https://github.com/HirohitoHigashi/mrubyc-nucleo-F401RE/raw/refs/heads/chapter05_i2c/Core/mrubyc/stm32f4_i2c.c | ruby -e 'print "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include \"mrubyc.h\""+ARGF.read.split(/===+/)[1]' |sed 's/default:/default: {}/' > outputbuffer.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arrayalloc.c >> outputbuffer.c
printf "\n#ifdef __cplusplus\n}\n#endif" >>outputbuffer.c
awk '/MRBC_TT_STRING:/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&!b&&/break/{$0=$0"\n#endif\n    case MRBC_TT_SYMBOL:\n      bufsiz += strlen(mrbc_symbol_cstr(&v[i]));\n      break;";a=0;b=1}a&&b&&/break/{$0=$0"\n#endif\n    case MRBC_TT_SYMBOL:\n      memcpy( pbuf, mrbc_symbol_cstr(&v[i]), strlen(mrbc_symbol_cstr(&v[i])) );\n      pbuf += mrbc_string_size(&v[i]);\n      break;\n";a=0;b=0}1' outputbuffer.c >temp.tmp && mv temp.tmp outputbuffer.c
# autogen
make autogen # require ruby
cd ..
mv src/ ~/Arduino/libraries/mrubyc_arduino
