Commit e81f1557 authored by Mol Thomas Folkert's avatar Mol Thomas Folkert
Browse files

wrote templates for generating code for both C and Zig

parent b23f5707
/// Header to be included in source, verbatim
/// No arguments.
const C_HEADER =
\\#include <stdio.h>
\\#include <stdint.h>
\\
;
const ZIG_HEADER = "";
/// General loop template used in below templates
/// Argument index:
/// 0 (string) = general type;
const C_GENERAL_LOOP =
\\ {0s} _acc = 0;
\\ for ({0s} i = _lo; i < _hi; ++i)
\\ _acc += _arr[i];
\\
;
const ZIG_GENERAL_LOOP =
\\ var _acc: {0s} = 0;
\\ var i: usize = _lo;
\\ while (i < _hi) : (i += 1) {{
\\ _acc += _arr[i];
\\ }}
\\
;
/// Loop testing template for comptime values of hi and arr
/// Argument index:
/// 0 (string) = general type, 1 (string) = function name
/// 2 (int) = array size, 3 (string) = array
const C_COMPTIME_LOOP_TEMPLATE =
\\{0s} {1s}() {{
\\ {0s} _lo = 0;
\\ {0s} _hi = {2i};
\\ {0s} _arr[{2i}] = {3s};
\\
++ C_GENERAL_LOOP ++
\\ return _acc;
\\}}
\\
;
const ZIG_COMPTIME_LOOP_TEMPPLATE =
\\fn {1s}() {0s} {{
\\ const _lo: {0s} = 0;
\\ const _hi: {0s} = {2i};
\\ const _arr = [_]{2i}{3i};
\\
++ ZIG_GENERAL_LOOP ++
\\ return _acc;
\\}}
\\
;
/// Loop testing template with volatile values of hi and arr
/// Argument index:
/// 0 (string) = general type, 1 (string) = function name
/// 2 (int) = array size, 3 (string) = array
const C_VOLATILE_LOOP_TEMPLATE =
\\{0s} {1s}() {{
\\ {0s} _lo = 0;
\\ volatile {0s} _hi = {2i};
\\ volatile {0s} _arr[{2i}] = {3s};
\\
++ C_GENERAL_LOOP ++
\\ return _acc;
\\}}
\\
;
// Can not use ZIG_GENERAL_LOOP because of the pointer syntax used.
const ZIG_VOLATILE_LOOP_TEMPLATE =
\\fn {1s}() {0s} {{
\\ const _lo: {0s} = 0;
\\ var _hi: {0s} = {2i};
\\ var _hi_p: *volatile {0s} = &_hi;
\\ var _arr = [_]{0s}{3i};
\\ var _arr_p = []volatile {0s} = &_arr;
\\
\\ var _acc: {0s} = 0;
\\ var i: usize = _lo;
\\ while (i < _hi_p.*) : (i += 1) {{
\\ _acc += _arr_p[i];
\\ }}
\\ return _acc;
\\}}
\\
;
/// Loop testing template with passed pointers
/// Argument index:
/// 0 (string) = general type, 1 (string) = function name
/// 2 (int) = array size, 3 (string) = array
const C_PASSED_LOOP_TEMPLATE =
\\{0s} _internal_passed_p({0s} _lo, {0s} *_hi, {0s} []* _arr) {{
\\ {0s} _acc = 0;
\\ for ({0s} i = _lo; i < *_hi, ++i) {{
\\ _acc += (*_arr)[i];
\\ return _acc;
\\}}
\\
\\{0s} {1s}() {{
\\ {0s} _lo = 0;
\\ {0s} _hi = {2i};
\\ {0s} _arr[{2i}] = {3s};
\\ return _internal_passed_p(_lo, &_hi, &_arr);
\\}}
\\
;
const ZIG_PASSED_LOOP_TEMPLATE =
\\fn _internal_passed_p(_lo: {0s}, _hi: *{0s}, _arr: *[]{0s}) {{
\\ var _acc: {0s} = 0;
\\ var i: usize = _lo;
\\ while (i < _hi.*) : (i += 1) {{
\\ _acc += _arr.*[i];
\\ }}
\\ return _acc;
\\}}
\\
\\fn {1s}() {0s} {{
\\ const _lo: {0s} = 0;
\\ var _hi: {0s} = {2i};
\\ var _arr = [_]{0s}{3i};
\\ return _internal_passed_p(_lo, &_hi, &_arr);
\\}}
\\
;
/// Loop testing template with values returned from passed function pointers
/// Argument index:
/// 0 (string) = general type, 1 (string) = function name
/// 2 (int) = array size, 3 (string) = array
const C_FUNCTION_LOOP_TEMPLATE =
\\{0s} _hi_fn() {{
\\ return {2s};
\\}}
\\
\\{0s} _fn_arr[{0s}] = {3s};
\\
\\{0s} *_arr_fn() {{
\\ return _fn_arr;
\\}}
\\
\\{0s} _internal_passed_fn({0s} (*_hi_fn_p)(), {0s} *(*_arr_fn_p)()) {{
\\ {0s} _lo = 0;
\\ {0s} _acc = 0;
\\ for ({0s} i = lo; i < (*_hi_fn_p)(); ++i)
\\ _acc += (*_arr_fn_p)()[i];
\\ return _acc;
\\}}
\\
\\{0s} {1s}() {{
\\ return _internal_passed_fn(&_hi_fn, &_arr_fn);
\\}}
\\
;
const ZIG_FUNCTION_LOOP_TEMPLATE =
\\fn _hi_fn() {0s} {{
\\ return {2s};
\\}}
\\
\\var _fn_arr = [_]{0s}{3i};
\\
\\fn _arr_fn() *{0s} {{
\\ return _fn_arr;
\\}}
\\
\\fn _internal_passed_fn(_hi_fn_p: fn () {0s}, _arr_fn_p: fn () *{0s}) {0s} {{
\\ const lo: {0s} = 0;
\\ var _acc: {0s} = 0;
\\ var i: {0s} = lo;
\\ while (i < _hi_fn_p()) : (i += 1) {
\\ _acc += _arr_fn_p()[i];
\\ }}
\\ return _acc;
\\}}
\\
\\fn {1s}() {0s} {{
\\ return _internal_passed_fn(_hi_fn, _arr_fn);
\\}}
\\
;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment