Омогућено постојање стандардне библиотеке

This commit is contained in:
kappa 2019-02-09 23:54:56 +01:00
parent 577c19940e
commit d86e1ba7c0
7 changed files with 92 additions and 70 deletions

View file

@ -5,26 +5,28 @@ VERSION = 0,8
# локација за инсталацију # локација за инсталацију
PREFIX = /usr/local PREFIX = /usr/local
LIBPREFIX = $(PREFIX)/lib
# флегови за C компајлер и линкер # флегови за C компајлер и линкер
CPPFLAGS = -D_POSIX_C_SOURCE=200200L CPPFLAGS = -D_POSIX_C_SOURCE=200200L
CFLAGS = -g -std=c99 -pedantic -Wall -O0 # CFLAGS = -g -std=c99 -pedantic -Wall -O0
# CFLAGS = -std=c99 -pedantic -Wall -O3 CFLAGS = -std=c99 -pedantic -Wall -O3
LDFLAGS = -lm -lc LDFLAGS = -lm -lc
CC = cc CC = cc
SRC = cirilisp.c read.c eval.c print.c util.c internals.c init.c C_SRC = cirilisp.c read.c eval.c print.c util.c internals.c
OBJ = $(SRC:.c=.o) TCH_SRC = инит.ћ
OBJ = $(C_SRC:.c=.o)
all: cirilisp all: cirilisp
.c.o: .c.o:
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< $(CC) -c $(CPPFLAGS) $(CFLAGS) $<
$(OBJ): util.h read.h eval.h print.h internals.h init.h $(OBJ): util.h read.h eval.h print.h internals.h $(TCH_SRC)
cirilisp: $(OBJ) cirilisp: $(OBJ) $(TCH_SRC)
$(CC) -o $@ $(OBJ) $(LDFLAGS) $(CC) -o $@ $(OBJ) $(LDFLAGS)
clean: clean:
@ -32,21 +34,24 @@ clean:
dist: clean dist: clean
mkdir -p cirilisp-$(VERSION) mkdir -p cirilisp-$(VERSION)
cp -r Makefile util.h read.h eval.h print.h $(SRC) cirilisp-$(VERSION) cp -r Makefile util.h read.h eval.h print.h $(C_SRC) cirilisp-$(VERSION)
tar -cf cirilisp-$(VERSION).tar cirilisp-$(VERSION) tar -cf cirilisp-$(VERSION).tar cirilisp-$(VERSION)
gzip cirilisp-$(VERSION).tar gzip cirilisp-$(VERSION).tar
rm -rf cirilisp-$(VERSION) rm -rf cirilisp-$(VERSION)
install: all install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin mkdir -p $(DESTDIR)$(PREFIX)/bin
mkdir -p $(DESTDIR)$(LIBPREFIX)/cirilisp
cp -f cirilisp $(DESTDIR)$(PREFIX)/bin cp -f cirilisp $(DESTDIR)$(PREFIX)/bin
chmod 755 $(DESTDIR)$(PREFIX)/bin/cirilisp chmod 755 $(DESTDIR)$(PREFIX)/bin/cirilisp
cp -f $(TCH_SRC) $(DESTDIR)$(LIBPREFIX)/cirilisp
# mkdir -p $(DESTDIR)$(MANPREFIX)/man1 # mkdir -p $(DESTDIR)$(MANPREFIX)/man1
# sed "s/VERSION/$(VERSION)/g" < cirilisp.1 > $(DESTDIR)$(MANPREFIX)/man1/dwm.1 # sed "s/VERSION/$(VERSION)/g" < cirilisp.1 > $(DESTDIR)$(MANPREFIX)/man1/dwm.1
# chmod 644 $(DESTDIR)$(MANPREFIX)/man1/cirilisp.1 # chmod 644 $(DESTDIR)$(MANPREFIX)/man1/cirilisp.1
uninstall: uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/cirilisp rm -f $(DESTDIR)$(PREFIX)/bin/cirilisp
rm -rf $(DESTDIR)$(LIBPREFIX)/cirilisp
# rm -f $(DESTDIR)$(MANPREFIX)/man1/cirilisp.1 # rm -f $(DESTDIR)$(MANPREFIX)/man1/cirilisp.1
.PHONY: all clean dist install uninstall .PHONY: all clean dist install uninstall

View file

@ -1,15 +1,65 @@
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "init.h" #include "internals.h"
#include "read.h" #include "read.h"
#include "eval.h" #include "eval.h"
#include "print.h" #include "print.h"
extern int eofStatus;
int load(char *pathname)
{
FILE *stream;
if ((stream = fopen(pathname, "r")) == NULL)
{
return 0;
}
while (TYPE(eval(read("", stream), globalEnv)) != EOFObject)
;
eofStatus = 0;
fclose(stream);
return 1;
}
void init()
{
if (setlocale(LC_ALL, "sr_RS.utf8") == NULL)
{
fprintf(stderr, "lokal programa se nije mogao podesiti na\
\"sr_RS.utf8\", proverite da li ste ga osposobili na vasem sistemu\n");
exit(0);
}
/* Омогућава библиотекама коришћеним у интерпретеру да протумаче српску
* ћирилицу */
globalEnv = createEnvironment(NULL);
addSymbolInternal("+", &addInt, 0);
addSymbolInternal("-", &subtractInt, 0);
addSymbolInternal("*", &multiplyInt, 0);
addSymbolInternal("/", &divideInt, 0);
addSymbolInternal("навод", &quoteInt, 1);
addSymbolInternal("дефиниши", &defineInt, 1);
addSymbolInternal("тачно->нетачно", &exactToInexactInt, 0);
addSymbolInternal("нетачно->тачно", &inexactToExactInt, 0);
addSymbolInternal("ламбда", &lambdaInt, 1);
addSymbolInternal("<", &lessInt, 0);
addSymbolInternal(">", &greaterInt, 0);
addSymbolInternal("ако", &ifInt, 1);
if (!load("/usr/local/lib/cirilisp/инит.ћ"))
{
fprintf(stderr, "Није пронађена стандардна ЋИРЛИСП библиотека\
\рограм се није могао правилно покренути\n");
exit(0);
}
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
init(); init();
while (print(eval(read("ШКЉ> "), globalEnv))); while (print(eval(read("ШКЉ> ", stdin), globalEnv)));
printf("\nДостигнут крај стрима.\nЗбогом и дођите нам опет!\n"); printf("\nДостигнут крај улазног тока.\nЗбогом и дођите нам опет!\n");
return 0; return 0;
} }

31
init.c
View file

@ -1,31 +0,0 @@
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include "internals.h"
void init()
{
if (setlocale(LC_ALL, "sr_RS.utf8") == NULL)
{
fprintf(stderr, "lokal se nije mogao podesiti na\
\"sr_RS.utf8\", proverite da li ste ga osposobili na vasem sistemu\n");
exit(0);
}
/* Омогућава библиотекама коришћеним у интерпретеру да протумаче српску
* ћирилицу */
globalEnv = createEnvironment(NULL);
addSymbolInternal("+", &addInt, 0);
addSymbolInternal("-", &subtractInt, 0);
addSymbolInternal("*", &multiplyInt, 0);
addSymbolInternal("/", &divideInt, 0);
addSymbolInternal("навод", &quoteInt, 1);
addSymbolInternal("дефиниши", &defineInt, 1);
addSymbolInternal("тачно->нетачно", &exactToInexactInt, 0);
addSymbolInternal("нетачно->тачно", &inexactToExactInt, 0);
addSymbolInternal("ламбда", &lambdaInt, 1);
addSymbolInternal("<", &lessInt, 0);
addSymbolInternal(">", &greaterInt, 0);
addSymbolInternal("ако", &ifInt, 1);
}

3
init.h
View file

@ -1,3 +0,0 @@
#pragma once
void init(void);

50
read.c
View file

@ -17,16 +17,16 @@ int isEscape(wchar_t c);
wint_t scanwc(FILE *stream); wint_t scanwc(FILE *stream);
wint_t unscanwc(wint_t c, FILE *stream); wint_t unscanwc(wint_t c, FILE *stream);
object getToken(); object getToken();
object macroFunction(wchar_t m); object macroFunction(wchar_t m, FILE *stream);
object read(char *prompt) object read(char *prompt, FILE *stream)
{ {
printf("%s", prompt); printf("%s", prompt);
wint_t c; wint_t c;
object result; object result;
while (iswspace(c = scanwc(stdin))) while (iswspace(c = scanwc(stream)))
; ;
if (c == WEOF) if (c == WEOF)
{ {
@ -34,12 +34,12 @@ object read(char *prompt)
} }
else if (isMacroC(c)) else if (isMacroC(c))
{ {
result = macroFunction(c); result = macroFunction(c, stream);
} }
else if (isEscape(c) || isConstituent(c)) else if (isEscape(c) || isConstituent(c))
{ {
unscanwc(c, stdin); unscanwc(c, stream);
result = getToken(); result = getToken(stream);
} }
else else
{ {
@ -48,7 +48,7 @@ object read(char *prompt)
if (TYPE(result) == unspecifiedObject) if (TYPE(result) == unspecifiedObject)
{ {
return read(""); return read("", stream);
/* уколико улаз функције није прави објекат (на пример уколико је учитан /* уколико улаз функције није прави објекат (на пример уколико је учитан
* коментар) покушавамо прочитати опет */ * коментар) покушавамо прочитати опет */
} }
@ -146,17 +146,17 @@ int lengthDigitArray(char *s)
return i; return i;
} }
object getToken() object getToken(FILE *stream)
{ {
object result; object result;
wchar_t *buffer = getBuffer(); wchar_t *buffer = getBuffer();
wint_t c; wint_t c;
int i = 0; int i = 0;
c = scanwc(stdin); c = scanwc(stream);
buffer[0] = towlower(c); buffer[0] = towlower(c);
if (isEscape(c)) if (isEscape(c))
{ {
while ((c = scanwc(stdin)) != WEOF && !isEscape(c)) while ((c = scanwc(stream)) != WEOF && !isEscape(c))
{ {
if (i + 2 >= bufferSize) if (i + 2 >= bufferSize)
{ {
@ -174,7 +174,7 @@ object getToken()
} }
else else
{ {
while (isConstituent(c = scanwc(stdin))) while (isConstituent(c = scanwc(stream)))
{ {
if (i + 1 >= bufferSize) if (i + 1 >= bufferSize)
{ {
@ -182,7 +182,7 @@ object getToken()
} }
buffer[++i] = towlower(c); buffer[++i] = towlower(c);
} }
unscanwc(c, stdin); unscanwc(c, stream);
buffer[++i] = L'\0'; buffer[++i] = L'\0';
} }
@ -248,7 +248,7 @@ wchar_t escapedWChar(wchar_t c)
} }
} }
object dispatchedChar(wint_t c) object dispatchedChar(wint_t c, FILE *stream)
{ {
object result; object result;
@ -260,7 +260,7 @@ object dispatchedChar(wint_t c)
wchar_t *buffer = getBuffer(); wchar_t *buffer = getBuffer();
int i = 0, n; int i = 0, n;
c = scanwc(stdin); c = scanwc(stream);
if (c == WEOF) if (c == WEOF)
{ {
SIGERR(unexpectedEOFError); SIGERR(unexpectedEOFError);
@ -271,8 +271,8 @@ object dispatchedChar(wint_t c)
} }
else else
{ {
unscanwc(c, stdin); unscanwc(c, stream);
while ((c = scanwc(stdin)) != WEOF && !iswspace(c)) while ((c = scanwc(stream)) != WEOF && !iswspace(c))
{ {
if (i + 1 >= bufferSize) if (i + 1 >= bufferSize)
{ {
@ -317,8 +317,8 @@ object dispatchedChar(wint_t c)
case L'|': case L'|':
for (;;) for (;;)
{ {
if (((c = scanwc(stdin)) == L'|' && if (((c = scanwc(stream)) == L'|' &&
(c = scanwc(stdin)) == L'#') || c == WEOF) (c = scanwc(stream)) == L'#') || c == WEOF)
{ {
break; break;
} }
@ -335,7 +335,7 @@ object dispatchedChar(wint_t c)
return result; return result;
} }
object macroFunction(wchar_t m) object macroFunction(wchar_t m, FILE *stream)
{ {
object result; object result;
object *listCurrent; object *listCurrent;
@ -349,7 +349,7 @@ object macroFunction(wchar_t m)
listCurrent = &result; listCurrent = &result;
for (;;) for (;;)
{ {
object currentObject = read(""); object currentObject = read("", stream);
if (TYPE(currentObject) == errorObject && if (TYPE(currentObject) == errorObject &&
ERR(currentObject) == unmatchedParenError) ERR(currentObject) == unmatchedParenError)
{ {
@ -438,7 +438,7 @@ object macroFunction(wchar_t m)
break; break;
case L'\'': case L'\'':
case L'`': case L'`':
expression = read(""); expression = read("", stream);
if (TYPE(expression) == errorObject) if (TYPE(expression) == errorObject)
{ {
SIGERR(ERR(expression)); SIGERR(ERR(expression));
@ -463,14 +463,14 @@ object macroFunction(wchar_t m)
break; break;
case L';': case L';':
TYPE(result) = unspecifiedObject; TYPE(result) = unspecifiedObject;
while ((c = scanwc(stdin)) != L'\n' && c != WEOF) while ((c = scanwc(stream)) != L'\n' && c != WEOF)
; ;
break; break;
case L'"': case L'"':
buffer = getBuffer(); buffer = getBuffer();
int i = 0; int i = 0;
while ((c = scanwc(stdin)) != L'"' && c != WEOF) while ((c = scanwc(stream)) != L'"' && c != WEOF)
{ {
if (i + 2 >= bufferSize) if (i + 2 >= bufferSize)
{ {
@ -478,7 +478,7 @@ object macroFunction(wchar_t m)
} }
if (c == L'\\') if (c == L'\\')
{ {
c = scanwc(stdin); c = scanwc(stream);
buffer[i++] = escapedWChar(c); buffer[i++] = escapedWChar(c);
} }
else else
@ -499,7 +499,7 @@ object macroFunction(wchar_t m)
STR(result) = s; STR(result) = s;
break; break;
case L'#': case L'#':
result = dispatchedChar(scanwc(stdin)); result = dispatchedChar(scanwc(stream), stream);
break; break;
} }

2
read.h
View file

@ -2,4 +2,4 @@
#include "util.h" #include "util.h"
object read(char *prompt); object read(char *prompt, FILE *stream);

1
инит.ћ Normal file
View file

@ -0,0 +1 @@
(дефиниши (= а б) (ако (> а б) #л (ако (< а б) #л #и)))