From 1afd0cdc7820d9c3a9ae032ea40545d7d32bf9bf Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Tue, 5 Jan 2021 22:16:50 +0300 Subject: String type added. --- examples/generated.csx | 100 ++++++++++++++++++------------- examples/translator.c | 160 +++++++++++++++++++++++++------------------------ 2 files changed, 143 insertions(+), 117 deletions(-) (limited to 'examples') diff --git a/examples/generated.csx b/examples/generated.csx index 14943b2..62fd2eb 100644 --- a/examples/generated.csx +++ b/examples/generated.csx @@ -1,47 +1,67 @@ +{ Base Utilities } + [set no [fn [x] [same x []]]] -[set outint [fn [n] [if [< n 0] - [do [out 45] [outint [neg n]]] - [if [< n 10] - [out [+ 48 n]] - [do - [outint [div n 10]] - [out [+ 48 [mod n 10]]] +[set id [fn [arg] arg]] +[set list [fn args args]] + +[set catrev [fn [a b] [if a [catrev [tail a] [pair [head a] b]] b]]] +[set rev [fn [l] [catrev l []]]] +[set cat [fn [a b] [catrev [rev a] b]]] + +[set map [fn [f l] [if l [pair [f [head l]] [map f [tail l]]]]]] +[set reduce [fn [f l] [if [no l] [] [if [no [tail l]] [head l] + [f [head l] [reduce f [tail l]]] +]]]] + +[set - [fn =[a rest] [+ a [reduce + [map neg rest]]]]] + + +{ Input-Output } + +[set newline [str [list 10]]] + +[set outint [fn [n] + [set zero 48] + [set minus 45] + [if [< n 0] + [do [out minus] [outint [neg n]]] + [if [< n 10] + [out [+ zero n]] + [do + [outint [div n 10]] + [out [+ zero [mod n 10]]] + ] ] ] -]]] -[set map [fn [f l] [if l [pair [f [head l]] [map f [tail l]]]]]] -[set id [fn args args]] -[set outstr [fn [str] [if str [do [out [head str]] [outstr [tail str]]]]]] -[set newline [fn [] [out 10]]] -[set instr [fn [] - [set c [in]] - [if [no [same c 10]] [pair c [instr]]] ]] -[set outints [fn [l] - [set outcontent [fn [l] - [if l [do [outint [head l]] [out 32] [outcontent [tail l]]]] + +[set outstr [fn [str] + [set outstrat [fn [str i len] [if [no [same i len]] [do + [out [str i]] + [outstrat str [+ i 1] len] + ]]]] + [outstrat str 0 [len str]] +]] + +[set output [fn objs [map [fn [obj] [if + [same [type obj] 'int] [outint obj] + [same [type obj] 'str] [outstr obj] +]] objs]]] + +[set instr [fn [] + [set instract [fn [] + [set c [in]] + [if [no [same c 10]] [pair c [instract]]] ]] - [out 91] [outcontent l] [out 93] + [str [instract]] ]] -[set rappend [fn [a b] [if a [rappend [tail a] [pair [head a] b]] b]]] -[set rev [fn [l] [rappend l []]]] -[set append [fn [a b] [rappend [rev a] b]]] -[set backwards [sx body [pair 'do [rev body]]]] - -[outstr "-= loaded =-"] -[newline] -[outstr "Hello, I am Casey Shawn Exton. What is your name?"] -[newline] -[outstr "> "] + + +{ The Program } + +[output "Hello, I am Casey Shawn Exton. " "What is your name?" newline] +[output "> "] [set name [instr]] -[outstr "Nice to meet you, "] -[outstr name] -[outstr "."] -[newline] -[outstr "I have to go. Goodbye!"] -[newline] - -[backwards - [outstr magic] - [set magic [rev "magic"]] -] +[output "Nice to meet you, " name "." newline] +[output "Your name is " [len name] " characters long." newline] +[output "I have to go. Goodbye!" newline] diff --git a/examples/translator.c b/examples/translator.c index 29f4d4e..01920c9 100644 --- a/examples/translator.c +++ b/examples/translator.c @@ -3,42 +3,83 @@ #include -void readlist() +int c; + + +void items(); + +void name(char a) +{ + printf("csx_name(\""); + if (a) putchar(a); + while (c != EOF && !isspace(c) && c != '[' && c != ']') { + if (c == '"' || c == '\\') putchar('\\'); + putchar(c); + c = getchar(); + } + printf("\")"); +} + +void num(char a) +{ + printf("csx_int("); + if (a) putchar(a); + c = getchar(); + while (c != EOF && isdigit(c)) { + putchar(c); + c = getchar(); + } + printf(")"); +} + +void pair() +{ + if ((c = getchar()) == '[') { + printf("csx_pair("); + items(); + printf("0)"); + c = getchar(); + } else name('='); +} + +void list() +{ + printf("csx_list("); + items(); + printf("0)"); + c = getchar(); +} + +void skip() +{ + while (isspace(c) || c == '{') { + if (isspace(c)) while (isspace(c = getchar())); + if (c == '{') { + int level = 1; + while (level && c != EOF) { + while ((c = getchar()) != '{' && c != '}' && c != EOF); + if (c == '{') ++level; + if (c == '}') --level; + } + if (c != EOF) c = getchar(); + } + } +} + +void items() { int first = 1; - int c = getchar(); + c = getchar(); + skip(); while (c != EOF && c != ']') { - if (c == '[') { - if (first) first = 0; - else putchar(','); - printf("csx_list("); - readlist(); - printf("0)"); - c = getchar(); - } else if (isdigit(c) || c == '-') { - if (first) first = 0; - else putchar(','); - printf("csx_int("); - putchar(c); - c = getchar(); - while (c != EOF && isdigit(c)) { - putchar(c); - c = getchar(); - } - printf(")"); - } else if (isspace(c)) { - while (isspace(c = getchar())); - } else if (c == '.') { - if (getchar() != '[') exit(1); - if (first) first = 0; - else putchar(','); - printf("csx_dot("); - readlist(); - printf("0)"); - c = getchar(); - } else if (c == '"') { - if (first) first = 0; - else putchar(','); + if (first) first = 0; else putchar(','); + if (c == '[') list(); + else if (c == '-') + if (isdigit(c = getchar())) num('-'); + else name('-'); + else if (isdigit(c)) num(c); + else if (c == '=') pair(); + else if (c == '"') { printf("csx_str(\""); c = getchar(); while (c != EOF && c != '"') { @@ -50,57 +91,22 @@ void readlist() printf("\")"); c = getchar(); } else if (c == '\'') { - if (first) first = 0; - else putchar(','); printf("csx_list(csx_name(\"quote\"),"); c = getchar(); - if (isspace(c)) exit(1); - else if (c != '[') { - printf("csx_name(\""); - while (c != EOF && !isspace(c) && c != '[' && c != ']') { - if (c == '"' || c == '\\') putchar('\\'); - putchar(c); - c = getchar(); - } - printf("\"),"); - } else if (c == '.') { - if (getchar() != '[') exit(1); - printf("csx_dot("); - readlist(); - printf("0)"); - c = getchar(); - } else { - printf("csx_list("); - readlist(); - printf("0),"); - c = getchar(); - } - printf("0)"); - } else { - if (first) first = 0; - else putchar(','); - printf("csx_name(\""); - while (c != EOF && !isspace(c) && c != '[' && c != ']') { - if (c == '\\') c = getchar(); - if (c == '"' || c == '\\') putchar('\\'); - putchar(c); - c = getchar(); - } - printf("\")"); - } + if (c == '[') list(); + else if (c == '=') pair(); + else name(0); + printf(",0)"); + } else name(0); + skip(); } if (!first) putchar(','); } int main() { - puts("#include "); - puts("int main()"); - puts("{"); - printf("csx_run(csx_list(csx_name(\"do\"),"); - readlist(); - puts("0));"); - puts("return 0;"); - puts("}"); + printf("#include \nint main(){csx_run(csx_list(csx_name(\"do\"),"); + items(); + printf("0));return 0;}\n"); return 0; } -- cgit v1.2.3