Skip to content

Commit

Permalink
Patch Tuesday - A
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodan58 committed Feb 13, 2024
1 parent e53809e commit 97e7143
Show file tree
Hide file tree
Showing 164 changed files with 1,541 additions and 1,009 deletions.
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
joy.exe
moy.tar
pars.c
pars.h
lexr.c
prim.c
prim.h
tabl.c
moy.tar
CMakeCache.txt
CMakeFiles
Makefile
cmake_install.cmake
joy.exe
*.o
bdwgc
46 changes: 28 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#
# module : CMakeLists.txt
# version : 1.18
# date : 11/06/23
# version : 1.20
# date : 01/23/24
#
cmake_minimum_required(VERSION 3.0)
project(Moy VERSION 1.0)
project(Joy VERSION 1.0)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
option(RUN_TESTS "Run standard tests" OFF)
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
option(RUN_TESTS "Run standard tests" ON)
else()
option(RUN_TESTS "Run standard tests" OFF)
endif()
find_package(BISON)
find_package(FLEX)
BISON_TARGET(MyParser ${CMAKE_SOURCE_DIR}/pars.y ${CMAKE_SOURCE_DIR}/pars.c
Expand All @@ -23,26 +27,32 @@ ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)
add_executable(joy main.c ${BISON_MyParser_OUTPUTS} ${FLEX_MyScanner_OUTPUTS}
scan.c repl.c modl.c ylex.c util.c exec.c eval.c xerr.c otab.c
prog.c writ.c read.c save.c parm.c arty.c)
add_dependencies(joy always)
add_custom_target(always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND sh tabl.sh .
COMMAND sh prim.sh .
)
add_definitions(-DLINK="\\"${CMAKE_EXE_LINKER_FLAGS}\\"")
add_definitions(-DVERS="BDW ${CMAKE_BUILD_TYPE} ${CMAKE_PROJECT_VERSION}")
#
# MSVC: cmake --build . --config Release
#
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CF "-DCOPYRIGHT -DGC_NOT_DLL -D_CRT_SECURE_NO_WARNINGS")
add_definitions(${CF} -DCOMP="${CMAKE_C_FLAGS_RELEASE} ${CF}" -DLINK="${CMAKE_EXE_LINKER_FLAGS}" -DVERS="${CMAKE_BUILD_TYPE} ${CMAKE_PROJECT_VERSION}")
target_link_libraries(joy gc-lib)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOPYRIGHT -DGC_NOT_DLL -D_CRT_SECURE_NO_WARNINGS")
add_definitions(-DCOMP="\\"${CMAKE_C_FLAGS}\\"")
target_link_libraries(joy bdwgc/Release/gc)
include_directories(bdwgc/include)
add_subdirectory(bdwgc)
else()
add_dependencies(joy always)
add_custom_target(always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND sh tabl.sh .
COMMAND sh prim.sh .)
set(CF "-DCOPYRIGHT -Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter")
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage") # debug, no optimization
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") # enabling coverage
add_definitions(${CF} -DCOMP="${CMAKE_C_FLAGS_DEBUG} ${CF}" -DLINK="${CMAKE_EXE_LINKER_FLAGS}" -DVERS="${CMAKE_BUILD_TYPE} ${CMAKE_PROJECT_VERSION}")
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${CF}")
add_definitions(-DCOMP="\\"${CMAKE_C_FLAGS_RELEASE}\\"")
else()
add_definitions(${CF} -DCOMP="${CMAKE_C_FLAGS_RELEASE} ${CF}" -DLINK="${CMAKE_EXE_LINKER_FLAGS}" -DVERS="${CMAKE_BUILD_TYPE} ${CMAKE_PROJECT_VERSION}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${CF} -g -O0 --coverage -fprofile-arcs -ftest-coverage") # debug, no optimization
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") # enabling coverage
add_definitions(-DCOMP="\\"${CMAKE_C_FLAGS_DEBUG}\\"")
endif()
target_link_libraries(joy m gc)
if(RUN_TESTS)
Expand Down
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,37 @@ This repository implements [Joy](https://github.com/Wodan58/Joy) and uses
[document](https://github.com/Wodan58/Moy/blob/master/doc/MOYimplJOY.md)
explains the raison d'être.

Changes
-------

Directives borrowed from [42minjoy](https://github.com/Wodan58/42minjoy) are:
`%INCLUDE`, `%LISTING`, `%PUT`, and `%TRACE`.
The build system requires new builtins in only one location: as a .c file in
the src-directory.

Build instructions
------------------

cd build
SOURCE_DATE_EPOCH=1047920271 cmake ..
cmake ..
cmake --build .

There is a customized version of usrlib.joy waiting in the build directory.
Build with MSVC
---------------

After installing bdwgc in the bdwgc-directory use the CMake GUI to uncheck all
boxes and then check the boxes in the lines that start with disable\_ and
check the last one: without\_libatomic\_ops.

cd build
cmake ..
cmake --build . --config Release
copy Release\joy.exe

Running
-------

There is a copy of usrlib.joy in the build directory.

See also
--------

Implementation|Dependencies
--------------|------------
[42minjoy](https://github.com/Wodan58/42minjoy)|
[joy0](https://github.com/Wodan58/joy0)|
[Joy](https://github.com/Wodan58/Joy)|
[joy1](https://github.com/Wodan58/joy1)|[BDW garbage collector](https://github.com/ivmai/bdwgc)

Expand Down
66 changes: 50 additions & 16 deletions arty.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
module : arty.c
version : 1.7
date : 11/09/23
version : 1.11
date : 02/12/24
*/
#include "globals.h"

Expand All @@ -12,40 +12,73 @@
are handled by returning -1. Unknown only means that it is considered
too difficult to try to figure out what the stack effect is.
*/
PRIVATE int aggr_size(Node node)
{
int num;
int64_t i, j;

switch (node.op) {
case LIST_:
return pvec_cnt(node.u.lis);

case BIGNUM_:
case STRING_:
return strlen(node.u.str);

case SET_:
for (num = 0, j = 1, i = 0; i < SETSIZE; i++, j <<= 1)
if (node.u.set & j)
num++;
return num;
}
return 0;
}

PUBLIC int arity(pEnv env, NodeList *quot, int num)
{
char *str;
Entry ent;
OpTable *tab;
int aggr, prog; /* step combinator */
NodeList *list;
Node node, prev;
Node node, prev, prevprev;

list = pvec_init();
pvec_copy(list, quot); /* make a copy */
prev.u.lis = 0;
prev.op = 0;
prevprev.u.lis = prev.u.lis = 0;
prevprev.op = prev.op = 0;
while (pvec_cnt(list)) {
list = pvec_pop(list, &node); /* read a node */
switch (node.op) {
case USR_:
ent = vec_at(env->symtab, node.u.ent);
if (ent.u.body && !pvec_getused(ent.u.body)) {
list = pvec_concat(list, ent.u.body);
pvec_setused(ent.u.body); /* prevent recursion */
}
break;
return -1; /* assume too difficult */
case ANON_FUNCT_:
str = operarity(node.u.proc); /* problem: lineair search */
if (env->bytecoding || env->compiling) {
tab = readtable(node.u.ent); /* symbol table is w/o arity */
str = tab->arity;
} else
str = operarity(node.u.proc); /* problem: lineair search */
for (; *str; str++)
if (*str == 'A') /* add */
num++;
else if (*str == 'D') { /* delete */
if (--num < 0)
return -1;
} else if (*str == 'P') { /* previous */
} else if (*str == 'P') { /* previous one */
if (prev.op != LIST_)
return -1;
if (prev.u.lis) /* prevent empty */
list = pvec_concat(list, prev.u.lis);
if (prev.u.lis) { /* skip empty */
prog = arity(env, prev.u.lis, 0); /* recursion */
if (prog < 0)
return -1;
num += prog;
}
} else if (*str == 'Q') { /* previous two */
if (prev.op != LIST_)
return -1;
if ((prog = arity(env, prev.u.lis, 1)) < 0) /* recursion */
return -1;
aggr = aggr_size(prevprev); /* size of aggregate */
num += aggr * prog;
} else if (*str == 'U') /* unknown */
return -1;
break;
Expand All @@ -63,6 +96,7 @@ PUBLIC int arity(pEnv env, NodeList *quot, int num)
num++;
break;
}
prevprev = prev;
prev = node;
}
return num;
Expand Down
4 changes: 2 additions & 2 deletions build/usrlib.joy
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ END. (* end HIDE and LIBRA *)

"usrlib is loaded\n" putchars.

standard-setting.
# standard-setting.

"../lib/inilib.joy" include.
(* assuming inilib.joy was included: *)
"agglib" libload.

DEFINE verbose == true. (* Example of over-riding inilib.joy *)
DEFINE verbose == true. (* Example of over-riding inilib.joy *)

(* END usrlib.joy *)
16 changes: 5 additions & 11 deletions doc/MOYimplJOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ Introduction
This page presents a note about the technicalities of this Joy implementation.
The mechanisms of this implementation differ from the
[reference implementation](https://github.com/Wodan58/Joy).
The language itself should be the same. Whatever is not the same should be
repaired, unless it is a consequence of using Flex and Bison.
The language itself should be the same.

Implementation details
======================

About the implementation: the aim is to leave the language untouched. That
means that all tests in the test2 directory and all examples in the lib
directory should behave exactly as in the reference implementation of Joy.

What is different in this implementation is the use of vectors instead of
linked lists and the stackless recursion. There are exceptions: `get` and
`fget` use `readfactor` that may call `readterm`, that calls `readfactor`.
This implementation uses vectors instead of linked lists and it recurses
without overflowing the stack.

The big advantage of stackless recursion is in the size of data structures that
can be handled. A program that builds a list of integers in idiomatic fashion
Expand All @@ -33,5 +27,5 @@ collector that causes the stack overflow. `Joy` has Cheney's algorithm
implemented, that is not recursive but still fails because `from-to-list` makes
use of `linrec` and `linrec` recurses.

This implementation is stackless and so succeeds where other implementations
fail. There is a downside: function calling is slower.
This implementation succeeds where other implementations fail.
There is a downside: function calling is slower.
Loading

0 comments on commit 97e7143

Please sign in to comment.