├── .github ├── FUNDING.yml └── workflows │ └── main.yml ├── .gitignore ├── .mocharc.json ├── CMakeLists.txt ├── COPYING.txt ├── Makefile ├── README.md ├── analysis ├── statistical_graphical_analysis.py ├── tests_analysis.py └── tests_runner.py ├── docker └── Dockerfile ├── make.sh ├── msg ├── BASE.cmake ├── COS_ADDER.gate ├── COS_AND.gate ├── COS_Addition.md ├── COS_Addition.pl ├── COS_AndLogic.md ├── COS_AndLogic.pl ├── COS_Begin.js ├── COS_Binary.js ├── COS_Binary.md ├── COS_CommentBase.pl ├── COS_CommentBase2.pl ├── COS_CommentBase3.pl ├── COS_Compare.js ├── COS_Compare.md ├── COS_Complex.js ├── COS_Cons.md ├── COS_Cons.pl ├── COS_D.gate ├── COS_DefineFunction.js ├── COS_DefineFunction.md ├── COS_Demo.scm ├── COS_Dot.js ├── COS_Double.pl ├── COS_Element.js ├── COS_Element.md ├── COS_Evaluation.js ├── COS_Evaluation.md ├── COS_Geo.scm ├── COS_Graph.js ├── COS_Graph.md ├── COS_Hash.pl ├── COS_If.md ├── COS_If.pl ├── COS_Implication.pl ├── COS_Intro.js ├── COS_Intro.md ├── COS_JAction.java ├── COS_JDoor.java ├── COS_JHashMap.java ├── COS_JList.java ├── COS_JListIterator.java ├── COS_JListIterator_impl.scm ├── COS_JList_impl.scm ├── COS_JList_test.scm ├── COS_JNamed.java ├── COS_JRobo.java ├── COS_JRoom.java ├── COS_JRoom_test.scm ├── COS_JThing.java ├── COS_JWorld.java ├── COS_JavaMain.java ├── COS_JavaPrep.scm ├── COS_JavaTest.java ├── COS_JavaValidate.scm ├── COS_Let.js ├── COS_Let.md ├── COS_License.scm ├── COS_List.js ├── COS_ListPre.js ├── COS_ListPre.md ├── COS_MARK.scm ├── COS_Map.js ├── COS_Map.md ├── COS_Message.scm ├── COS_MultipleParameter.js ├── COS_Multiplication.md ├── COS_Multiplication.pl ├── COS_Mutable.md ├── COS_Mutable.pl ├── COS_MutableStructure.js ├── COS_NOR.gate ├── COS_NOT.gate ├── COS_NewType.js ├── COS_NotLogic.md ├── COS_NotLogic.pl ├── COS_OR.gate ├── COS_OSC.gate ├── COS_OrLogic.md ├── COS_OrLogic.pl ├── COS_Pure.pl ├── COS_Quantifier.pl ├── COS_Quote.js ├── COS_SR.gate ├── COS_Series.js ├── COS_Series.md ├── COS_Set.js ├── COS_Set.md ├── COS_Solve.js ├── COS_Subtraction.md ├── COS_Subtraction.pl ├── COS_Translate.js ├── COS_TrueFalse.md ├── COS_TrueFalse.pl ├── COS_Turing.js ├── COS_Turing.md ├── COS_TypedGraph.pl ├── COS_Unless.scm ├── COS_UnlessProbe.scm ├── DEPEND.cmake ├── PARTIAL.cmake ├── README.cmake ├── cosmic.js ├── cosmic.pm └── names.json ├── package.json ├── src ├── CMakeLists.txt ├── Makefile ├── assemble │ ├── CosmicDrive.js │ ├── assemble2json.js │ ├── cosmicos.js │ ├── evaluate.js │ ├── primer.js │ └── vocab.js ├── attic │ ├── sound.cpp │ └── whitespace.cpp ├── cmake │ ├── assemble.cmake │ ├── convert_gate.cmake │ ├── convert_java.cmake │ ├── convert_js.cmake │ ├── convert_md.cmake │ ├── convert_pl.cmake │ ├── convert_scm.cmake │ ├── node.cmake │ ├── typescript.cmake │ ├── wc.cmake │ └── wrap.cmake ├── cmd │ ├── Eval.ts │ ├── cosh.ts │ └── cosmsg.js ├── config.json ├── cosmicos │ ├── AllNumericCodec.hx │ ├── BigInteger.hx │ ├── BitString.hx │ ├── CacheCodec.hx │ ├── ChainCodec.hx │ ├── Codec.hx │ ├── Codec.ts │ ├── Complex.hx │ ├── Config.hx │ ├── Cons.hx │ ├── CosDefine.hx │ ├── CosFunction.hx │ ├── Cursor.hx │ ├── Evaluate.hx │ ├── EvaluateCodec.hx │ ├── ExternalVocab.hx │ ├── FourSymbolCodec.hx │ ├── FourSymbolCodecV2.ts │ ├── GlyphCode.hx │ ├── GlyphCode.ts │ ├── HumanSymbolCodec.hx │ ├── I32.hx │ ├── ManuscriptStyle.hx │ ├── Memory.hx │ ├── NormalizeCodec.hx │ ├── OghamStyle.hx │ ├── Parse.hx │ ├── ParseCodec.hx │ ├── PreprocessCodec.hx │ ├── Rename.ts │ ├── Sound.hx │ ├── SpiderImage.hx │ ├── SpiderScrawl.hx │ ├── State.hx │ ├── Statement.hx │ ├── Statement.ts │ ├── TranslateCodec.hx │ ├── UnflattenCodec.hx │ ├── Vocab.hx │ ├── Vocab.ts │ └── VocabMeta.hx ├── filter │ ├── drawgate-ppm.pl │ ├── drawgate-txt.pl │ ├── java-comment.pl │ └── wrap.pl ├── font │ ├── OctoGlyph.ts │ ├── SpiderScrawl.d.ts │ ├── fontcustom.yml │ ├── generate_glyphs.ts │ ├── glyphs_to_svg.sh │ ├── msg2glyphs.js │ └── msg2ogham.js ├── gate │ ├── UnlessDraw.ts │ ├── UnlessGrid.ts │ ├── UnlessGridExamples.ts │ └── gate.ts ├── java │ ├── CMakeLists.txt │ ├── Fritzifier.java │ ├── GridLoader.java │ ├── Unless.java │ ├── UnlessDriver.java │ ├── UnlessGrid.java │ └── UnlessNet.java ├── make_without_docker.sh ├── prepare_node.sh ├── tests │ ├── test_audio.js │ └── test_scrawl.js └── webpack.config.js ├── tests ├── CMakeLists.txt ├── EvaluateTest.hx ├── FourSymbolTest.hx ├── Main.hx ├── NormalizeTest.hx ├── ParseTest.hx ├── TranslateTest.hx ├── UnflattenTest.hx └── test_basics.ts ├── tsconfig.json ├── variant ├── iconic.cmake ├── iconic │ └── vocab.json ├── nested.cmake ├── standard.cmake └── tiny.cmake └── wiki_images ├── COS_message_picture.png └── COS_zipfs_law.png /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [paulfitz] 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | # Allows running this workflow manually from the Actions tab 10 | workflow_dispatch: 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-18.04 15 | strategy: 16 | matrix: 17 | haxe-version: [3.4.7] 18 | python-version: [3.8] 19 | node-version: [12.x] 20 | 21 | steps: 22 | - name: Install system packages 23 | run: sudo apt update && sudo apt install -y cmake libbcel-java openjdk-8-jdk-headless libgd-gd2-perl 24 | 25 | - uses: actions/checkout@v2 26 | 27 | - name: Use Node.js ${{ matrix.node-version }} 28 | uses: actions/setup-node@v1 29 | with: 30 | node-version: ${{ matrix.node-version }} 31 | 32 | - name: Set up Python ${{ matrix.python-version }} 33 | uses: actions/setup-python@v2 34 | with: 35 | python-version: ${{ matrix.python-version }} 36 | 37 | - name: Install numpy and matplotlib 38 | run: | 39 | python -m pip install --upgrade pip 40 | pip install numpy 41 | pip install matplotlib 42 | 43 | - name: Set up Haxe ${{ matrix.haxe-version }} 44 | uses: krdlab/setup-haxe@v1 45 | with: 46 | haxe-version: ${{ matrix.haxe-version }} 47 | 48 | - name: Install Node.js packages 49 | run: npm install 50 | 51 | - name: make tiny 52 | run: src/make_without_docker.sh tiny 53 | 54 | - name: make test 55 | run: src/make_without_docker.sh test 56 | 57 | - name: analysis test 58 | run: python analysis/tests_runner.py 59 | 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build*/ 2 | package 3 | INTERNAL.txt 4 | *~ 5 | -------------------------------------------------------------------------------- /.mocharc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extension": ["js", "ts"], 3 | "require": "ts-node/register" 4 | } 5 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(cosmicos NONE) 4 | 5 | add_subdirectory(src) 6 | 7 | enable_testing() 8 | add_subdirectory(tests) 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | ./make.sh 3 | 4 | help: 5 | ./make.sh help 6 | 7 | dock: 8 | cd docker && cp ../package.json . && docker build -t paulfitz/cosmicos_builder . 9 | 10 | %: 11 | ./make.sh $* 12 | -------------------------------------------------------------------------------- /analysis/tests_runner.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Sat Apr 23 16:03:36 2022 5 | 6 | @author: joha2 7 | 8 | Boilerplate code for unittest 9 | """ 10 | 11 | import unittest 12 | 13 | import tests_analysis 14 | 15 | loader = unittest.TestLoader() 16 | suite = unittest.TestSuite() 17 | 18 | suite.addTests(loader.loadTestsFromModule(tests_analysis)) 19 | 20 | runner = unittest.TextTestRunner(verbosity=3) 21 | result = runner.run(suite) -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # This creates an image that is good enough for building CosmicOS 2 | 3 | FROM ubuntu:18.04 4 | 5 | # enough for main message 6 | RUN \ 7 | apt-get update && \ 8 | apt-get install -y --no-install-recommends libbcel-java openjdk-8-jdk libgd-perl \ 9 | haxe cmake make && \ 10 | rm -rf /var/lib/apt/lists/* 11 | 12 | RUN \ 13 | apt-get update && \ 14 | apt-get install -y curl && \ 15 | curl -sL https://deb.nodesource.com/setup_12.x | bash && \ 16 | apt-get install -y nodejs && \ 17 | rm -rf /var/lib/apt/lists/* 18 | 19 | # lots of stuff needed for node canvas for old spider script 20 | RUN \ 21 | apt-get update && \ 22 | apt-get install -y --no-install-recommends libcairo2-dev libjpeg-dev libpango1.0-dev \ 23 | libgif-dev build-essential g++ && \ 24 | rm -rf /var/lib/apt/lists/* 25 | 26 | ENV NODE_PATH=/usr/local/lib/node_modules:/usr/log/node_modules 27 | 28 | RUN mkdir /tmp/.npm-global 29 | ENV PATH=/tmp/.npm-global/bin:$PATH 30 | ENV NPM_CONFIG_PREFIX=/tmp/.npm-global 31 | 32 | # I promise I'll start using package.json one day 33 | RUN npm install canvas -g --unsafe 34 | 35 | RUN \ 36 | apt-get update && \ 37 | apt-get install -y --no-install-recommends imagemagick potrace fontforge-nox && \ 38 | rm -rf /var/lib/apt/lists/* 39 | 40 | # oh my word, looks like I used a ruby gem to make font files for the spider font 41 | # and eek that fontcustom gem needs some unpackaged woff stuff 42 | RUN \ 43 | apt-get update && \ 44 | apt-get install -y --no-install-recommends ruby ruby-dev git && \ 45 | gem install fontcustom --no-ri --no-rdoc && \ 46 | rm -rf /var/lib/apt/lists/* && \ 47 | git clone https://github.com/bramstein/sfnt2woff-zopfli.git sfnt2woff-zopfli && cd sfnt2woff-zopfli && make && mv sfnt2woff-zopfli /usr/local/bin/sfnt2woff && cd .. && \ 48 | git clone --recursive https://github.com/google/woff2.git && cd woff2 && make clean all && mv woff2_compress /usr/local/bin/ && mv woff2_decompress /usr/local/bin/ && cd .. && \ 49 | rm -rf sfnt2woff-zopfli woff2 50 | 51 | # handy to be able to edit configuration options 52 | RUN \ 53 | apt-get update && \ 54 | apt-get install -y --no-install-recommends cmake-curses-gui && \ 55 | rm -rf /var/lib/apt/lists/* 56 | 57 | WORKDIR /cosmicos 58 | -------------------------------------------------------------------------------- /make.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | hash docker 2>/dev/null || { 6 | echo "Docker not found. Please install." 7 | exit 1 8 | } 9 | 10 | if [ -e build ]; then 11 | if [ ! -e build/docker_build.txt ]; then 12 | echo "Please remove build directory." 13 | exit 1 14 | fi 15 | fi 16 | 17 | mkdir -p build 18 | touch build/docker_build.txt 19 | 20 | if [[ -n "$UID" && -n "$USER" ]]; then 21 | # run docker as current user for convenience 22 | args="-u=$UID:$(id -g $USER)" 23 | fi 24 | 25 | # start a container in the background, just to make builds more responsive 26 | docker ps -f name=cosmicos_build | grep -q cosmicos || { 27 | docker run --rm --name cosmicos_build -v $PWD:/cosmicos paulfitz/cosmicos_builder src/prepare_node.sh 28 | docker run -dit --rm $args --name cosmicos_build -v $PWD:/cosmicos -v /tmp/docker_npm:/.npm -v $PWD/node_modules_docker:/cosmicos/node_modules paulfitz/cosmicos_builder /bin/bash 29 | } 30 | 31 | # run our actual build command, finally 32 | if [[ "$1" = "run" ]]; then 33 | shift 34 | docker exec -e VERBOSE=$VERBOSE cosmicos_build "$@" 35 | elif [[ "$1" = "console" ]]; then 36 | shift 37 | docker exec -it -e VERBOSE=$VERBOSE cosmicos_build /bin/bash 38 | elif [[ "$1" = "mocha" ]]; then 39 | docker exec -e VERBOSE=$VERBOSE cosmicos_build node_modules/.bin/mocha tests/test_basics.ts 40 | else 41 | docker exec -e VERBOSE=$VERBOSE cosmicos_build src/make_without_docker.sh "$@" 42 | fi 43 | -------------------------------------------------------------------------------- /msg/BASE.cmake: -------------------------------------------------------------------------------- 1 | set(COSMIC_VOCAB) 2 | set(COSMIC_USE_FLATTENER true) 3 | -------------------------------------------------------------------------------- /msg/COS_ADDER.gate: -------------------------------------------------------------------------------- 1 | 10 3 1 0 2 | 1 4 0 1 IN1 3 | 3 4 0 1 4 | 5 4 0 1 5 | 7 4 0 1 6 | 9 4 0 1 7 | 17 4 0 1 8 | 19 4 0 1 9 | 6 5 1 0 10 | 10 5 1 0 11 | 16 5 -1 0 12 | 9 6 0 1 13 | 11 6 0 1 14 | 13 6 0 1 15 | 15 6 0 1 16 | 6 7 1 0 17 | 12 7 -1 0 18 | 14 7 1 0 19 | 3 8 0 1 20 | 5 8 0 1 21 | 7 8 0 1 22 | 4 9 -1 0 23 | 8 9 1 0 24 | 12 9 -1 0 25 | 14 9 1 0 26 | 7 10 0 1 27 | 9 10 0 1 28 | 11 10 0 1 29 | 13 10 0 1 30 | 15 10 0 1 31 | 4 11 -1 0 32 | 12 11 -1 0 33 | 16 11 1 0 34 | 1 12 0 1 IN2 35 | 3 12 0 1 36 | 5 12 0 1 37 | 7 12 0 1 38 | 9 12 0 1 39 | 11 12 0 1 40 | 17 12 0 1 41 | 19 12 0 1 42 | 12 13 -1 0 43 | 44 | -------------------------------------------------------------------------------- /msg/COS_AND.gate: -------------------------------------------------------------------------------- 1 | 1 2 0 1 IN1 2 | 3 2 0 1 3 | 4 3 1 0 4 | 10 3 1 0 5 | 3 4 0 1 6 | 5 4 0 1 7 | 7 4 0 1 8 | 9 4 0 1 9 | 10 5 1 0 10 | 3 6 0 1 11 | 5 6 0 1 12 | 7 6 0 1 13 | 9 6 0 1 14 | 4 7 -1 0 15 | 10 7 1 0 16 | 1 8 0 1 IN2 17 | 3 8 0 1 18 | 11 8 0 1 19 | 13 8 0 1 20 | 15 8 0 1 21 | 17 8 0 1 22 | -------------------------------------------------------------------------------- /msg/COS_Addition.md: -------------------------------------------------------------------------------- 1 | # MATH introduce addition 2 | 3 | Let's introduce some arithmetic, to show off still more sentence structure. 4 | We show sentences of the mathematical form `X = Y + Z`, which in our message look like 5 | `= X | + Y Z`. From this, and the negation lesson, the listener will hopefully 6 | start picking up on how to chain operations. 7 | 8 | If the listener didn't already have a pretty clear idea of what `=` is, 9 | then these sentences could just as easily be interpreted as being about subtraction. 10 | Even having an idea of `=`, syntax is still fuzzy enough that this lesson may not 11 | be unambiguous by itself. 12 | -------------------------------------------------------------------------------- /msg/COS_Addition.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowAdditionLesson { 7 | my $txt = ""; 8 | $txt .= ShowLine(Op("intro","+")); 9 | for (my $i=0; $i<10; $i++) 10 | { 11 | my $r = irand(5); 12 | my $r2 = irand(5); 13 | $txt .= ShowLine(TailOp2("=", 14 | ShowUnary($r+$r2), 15 | Op2("+", 16 | ShowUnary($r), 17 | ShowUnary($r2)))); 18 | } 19 | return $txt; 20 | }; 21 | 22 | 23 | ShowLesson(ShowAdditionLesson()); 24 | 25 | -------------------------------------------------------------------------------- /msg/COS_AndLogic.md: -------------------------------------------------------------------------------- 1 | # MATH introduce the AND logical operator 2 | 3 | We continue introducing symbols related to math and logic. Now we will often 4 | be able to both define them and give examples, so the listener has multiple 5 | paths to understanding. 6 | 7 | Here is `and`, which evaluates to `true` if both its two arguments are `true`, 8 | and `false` otherwise. We don't talk about wbat happens if you pass it integers 9 | or something funky. There'd be value in getting into talking about types, but 10 | it might be a bit much just now. 11 | -------------------------------------------------------------------------------- /msg/COS_AndLogic.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowAndLogicLesson { 7 | my $txt = ""; 8 | 9 | $txt .= ShowLine(Op("intro","and")); 10 | 11 | $txt .= 'define and | ? x | ? y | if $x $y $false;' . "\n"; 12 | 13 | $txt .= 'not | and $false $false;' . "\n"; 14 | $txt .= 'not | and $false $true;' . "\n"; 15 | $txt .= 'not | and $true $false;' . "\n"; 16 | $txt .= 'and $true $true;' . "\n"; 17 | $txt .= '= $false | and $false $false;' . "\n"; 18 | $txt .= '= $false | and $false $true;' . "\n"; 19 | $txt .= '= $false | and $true $false;' . "\n"; 20 | $txt .= '= $true | and $true $true;' . "\n"; 21 | 22 | for (my $i=0; $i<10; $i++) 23 | { 24 | $txt .= ShowLine(Op2("and",ShowTrueComparison(),ShowTrueComparison())); 25 | } 26 | 27 | for (my $i=0; $i<5; $i++) 28 | { 29 | $txt .= ShowLine(Op1("not", 30 | Op2("and", 31 | ShowTrueComparison(), 32 | ShowFalseComparison()))); 33 | } 34 | 35 | for (my $i=0; $i<5; $i++) 36 | { 37 | $txt .= ShowLine(Op1("not", 38 | Op2("and", 39 | ShowFalseComparison(), 40 | ShowTrueComparison()))); 41 | } 42 | 43 | for (my $i=0; $i<5; $i++) 44 | { 45 | $txt .= ShowLine(Op1("not", 46 | Op2("and", 47 | ShowFalseComparison(), 48 | ShowFalseComparison()))); 49 | } 50 | 51 | for (my $i=0; $i<10; $i++) 52 | { 53 | my $t1 = irand(2); 54 | my $t2 = irand(2); 55 | my $c1 = ""; 56 | my $c2 = ""; 57 | if ($t1==1) 58 | { 59 | $c1 = ShowTrueComparison(); 60 | } 61 | else 62 | { 63 | $c1 = ShowFalseComparison(); 64 | } 65 | if ($t2==1) 66 | { 67 | $c2 = ShowTrueComparison(); 68 | } 69 | else 70 | { 71 | $c2 = ShowFalseComparison(); 72 | } 73 | my $c = Op2("and",$c1,$c2); 74 | 75 | if (!(($t1==1)&&($t2==1))) 76 | { 77 | $c = Op1("not",$c); 78 | } 79 | $txt .= ShowLine($c); 80 | } 81 | 82 | return $txt; 83 | } 84 | 85 | 86 | ShowLesson(ShowAndLogicLesson()); 87 | 88 | -------------------------------------------------------------------------------- /msg/COS_Begin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.header('MATH', 'show how to execute a sequence of instructions'); 6 | 7 | cos.add(`intro begin`); 8 | cos.add(`define translate:begin:prev $translate`); 9 | cos.add(`define translate | let ((prev $translate:begin:prev)) | ? x | 10 | if (not | function? $x) (prev $x) | 11 | if (not | = (head $x) begin) (prev $x) | 12 | translate | vector (vector ? x (vector last (vector x))) (prepend vector | tail $x)`); 13 | cos.add(`= 4 | begin 1 7 2 4`); 14 | cos.add(`= 6 | begin (set! $demo:make-cell:x 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:x)`); 15 | cos.add(`= 88 | begin (set! $demo:make-cell:y 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:y)`); 16 | cos.add(`= 4 | begin (set! $demo:make-cell:x 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:x) 4`); 17 | -------------------------------------------------------------------------------- /msg/COS_Binary.js: -------------------------------------------------------------------------------- 1 | 2 | var cos = require("./cosmic"); 3 | cos.language(2); 4 | cos.seed(42); 5 | 6 | var lst = []; 7 | for (var i=0; i<16; i++) { 8 | cos.add(["=",i,cos.unary(i)]); 9 | lst.push(i); 10 | } 11 | var v = 1; 12 | for (var i=0; i<5; i++) { 13 | cos.add(["=",v,cos.unary(v)]); 14 | v = v*2; 15 | } 16 | 17 | var lst2 = cos.permute(lst); 18 | for (var i=0; i<16; i++) { 19 | var j = lst2[i]; 20 | cos.add(["=",j,cos.unary(j)]); 21 | } 22 | 23 | var prev = {}; 24 | for (var i=0; i<8; i++) { 25 | var done = false; 26 | while (!done) { 27 | var r = cos.irand(8); 28 | var r2 = cos.irand(8); 29 | var idx = 100*r + r2; 30 | if (!prev[idx]) { 31 | cos.add(["=",cos.unary(r+r2),[-1,"+",cos.unary(r),cos.unary(r2)]]); 32 | cos.add(["=",r+r2,[-1,"+",r,r2]]); 33 | prev[idx] = true; 34 | done = true; 35 | } 36 | } 37 | } 38 | 39 | prev = {}; 40 | for (var i=0; i<8; i++) { 41 | var done = false; 42 | while (!done) { 43 | var r = cos.irand(4); 44 | var r2 = cos.irand(4); 45 | var idx = 100*r + r2; 46 | if (!prev[idx]) { 47 | cos.add(["=",cos.unary(r*r2),[-1,"*",cos.unary(r),cos.unary(r2)]]); 48 | cos.add(["=",r*r2,[-1,"*",r,r2]]); 49 | prev[idx] = true; 50 | done = true; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /msg/COS_Binary.md: -------------------------------------------------------------------------------- 1 | # MATH introduce non-unary representation of numbers 2 | 3 | Switch from unary numbers to another representation. The best representation 4 | will depend on the details of how the message is being transmitted, and the 5 | rest of the message doesn't depend on that choice for correctness (though the 6 | choice will have implications for how easy the message will be to interpret). 7 | As a base-line, imagine we use a binary representation. 8 | 9 | It isn't important for the listener to understand, but it might be worth explaining 10 | at this point how the unary representation worked. In fact there's no special 11 | syntax used, just three objects: 12 | 13 | * The number `0`. 14 | * The number `1`. 15 | * A function (called `unary` in English) that takes a value and: 16 | - If passed `0`, the function returns `0` 17 | - If passed `1`, the function returns another function, just like itself, 18 | except with any ultimate return value increased by `1`. 19 | 20 | Using syntax defined later in the message, `unary` could be defined as: 21 | 22 | ``` 23 | @ unary-v | ? v | ? x | if (= $x 0) $v (unary-v | + $v 1); 24 | @ unary | unary-v 0; 25 | ``` 26 | 27 | If you know Lisp/Scheme/etc, just read `@` as `define`, `?` as 28 | `lambda`, and `|` as opening a parenthesis that gets closed at the end 29 | of the statement. 30 | 31 | Anyway, all of this is a digression, but it is worth knowing that as much as possible 32 | the message is built from itself, so that in the end everything dovetails nicely. 33 | 34 | -------------------------------------------------------------------------------- /msg/COS_CommentBase.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | print "# NOTE end of part 1, start of part 2\n"; 7 | print "# The following parts of the message are experimental, and not\n"; 8 | print "# carefully integrated with the main body\n"; 9 | 10 | print "(intro part2);\n"; 11 | 12 | 13 | -------------------------------------------------------------------------------- /msg/COS_CommentBase2.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | print "# NOTE end of part 2, start of part 3\n"; 7 | print "# The following parts of the message are the beginnings\n"; 8 | print "# of embedding an alternate visual primer\n"; 9 | 10 | print "(intro part3);\n"; 11 | 12 | 13 | -------------------------------------------------------------------------------- /msg/COS_CommentBase3.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | print "# NOTE end of part 3, start of part 4\n"; 7 | print "# The following parts of the message start\n"; 8 | print "# to introduce some self-reference into the message\n"; 9 | 10 | print "(intro part4);\n"; 11 | 12 | 13 | -------------------------------------------------------------------------------- /msg/COS_Compare.js: -------------------------------------------------------------------------------- 1 | 2 | var cos = require("./cosmic"); 3 | 4 | cos.language(2); 5 | cos.seed(42); 6 | 7 | cos.add(["intro","="]); 8 | 9 | var examples = [1, 2, 3, 4, 5, 6, 7, 8, 1, 6, 2]; 10 | var examples2 = []; 11 | for (var i=0; i"]); 24 | cos.add(["intro","<"]); 25 | 26 | var prev = ""; 27 | for (var i=1; i<=4; i++) { 28 | for (var j=1; j<=4; j++) { 29 | var r = j; 30 | var r2 = i; 31 | var idx = r*100+r2; 32 | var cmp = "="; 33 | if (rr2) { 36 | cmp = "<"; 37 | } 38 | cos.add([cmp,cos.unary(r2),cos.unary(r)]); 39 | prev[idx] = true; 40 | } 41 | } 42 | 43 | cos.doc("Add some random examples."); 44 | 45 | prev = {}; 46 | for (var i=0; i<=10; i++) { 47 | var done = false; 48 | while (!done) { 49 | var r = cos.irand(10)+1; 50 | var r2 = cos.irand(r); 51 | var idx = r*100+r2; 52 | if (!prev[idx]) { 53 | cos.add([">",cos.unary(r),cos.unary(r2)]); 54 | prev[idx] = true; 55 | done = true; 56 | } 57 | } 58 | } 59 | 60 | prev = {}; 61 | for (var i=0; i<=10; i++) { 62 | var done = false; 63 | while (!done) { 64 | var r = cos.irand(10)+1; 65 | var r2 = cos.irand(r); 66 | var idx = r*100+r2; 67 | if (!prev[idx]) { 68 | cos.add(["<",cos.unary(r2),cos.unary(r)]); 69 | prev[idx] = true; 70 | done = true; 71 | } 72 | } 73 | } 74 | 75 | cos.doc("Even more random examples. We shouldn't be shy about piling on examples " + 76 | "at this early stage of the message. Even just the repetition of the sentence " + 77 | "structure with many small variations could help guide the listener at a more " + 78 | "fundamental level than what we're ostensibly trying to communicate here."); 79 | 80 | prev = ""; 81 | for (var i=0; i<=20; i++) { 82 | var done = false; 83 | while (!done) { 84 | var r = cos.irand(5); 85 | var r2 = cos.irand(5); 86 | var idx = r*100+r2; 87 | if (!prev[idx]) { 88 | var cmp = "="; 89 | if (r>r2) { 90 | cmp = ">"; 91 | } else if (r= 1 | list-length $x:list) $true | 11 | and (= (list-ref $x:list 0) (list-ref $x:list 1)) 12 | (all-equal | tail $x:list)`); 13 | cos.add("all-equal | vector 2 2 2"); 14 | cos.add("not | all-equal | vector 2 2 1"); 15 | cos.add("not | all-equal | vector 2 1 2"); 16 | cos.add("not | all-equal | vector 1 2 2"); 17 | 18 | cos.intro("sum"); 19 | cos.add("define sum | reduce $+"); 20 | 21 | cos.add("intro i"); 22 | cos.add("= (minus 1) | * $i $i"); 23 | 24 | cos.add("define complex | ? x | ? y | + $x | * $y $i"); 25 | 26 | cos.add("= (complex 5 6) | + (complex 3 2) (complex 2 4)"); 27 | 28 | cos.add("= (complex 7 22) | * (complex 5 4) (complex 3 2)"); 29 | 30 | cos.add("= (complex 10 8) | * (complex 5 4) 2"); 31 | cos.add("= (complex 10 8) | * 2 (complex 5 4)"); 32 | 33 | cos.doc("should work through how to divide complex numbers (multiply by conjugate)"); 34 | cos.add("= (complex (frac 6 25) (frac 17 25)) | frac (complex 3 2) (complex 4 | minus 3)"); 35 | 36 | cos.add(` 37 | all-equal | vector 38 | (+ 7 | * 22 $i) 39 | (* (+ 5 | * 4 $i) (+ 3 | * 2 $i)) 40 | (sum | vector 41 | (* 5 3) 42 | (* 5 (* 2 $i)) 43 | (* (* 4 $i) 3) 44 | (* (* 4 $i) (* 2 $i))) 45 | (sum | vector 46 | 15 (* 10 $i) (* 12 $i) (minus 8)) 47 | (sum | vector 48 | (+ 15 (minus 8)) (* (+ 10 12) $i))`); 49 | 50 | cos.doc("Hint at Euler's identity"); 51 | cos.add(`float:= 0 | + 1 | exp:hat | * $pi $i`); 52 | -------------------------------------------------------------------------------- /msg/COS_Cons.md: -------------------------------------------------------------------------------- 1 | # MATH introduce pairs 2 | 3 | Now we introduce our first data structure. The expression `cons X Y` stores `X` and `Y` 4 | in a pair. We can then pull `X` out from the pair with `car (cons X Y)`, and we can 5 | get `Y` out from the pair with `cdr (cons X Y)`. Apologies for the arcane names, 6 | they are inherited from Lisp (and they'll be encoded as something else in the 7 | message anyway). 8 | 9 | We give a definition of `cons` that is a bit funky. The `cons X Y` expression 10 | constructs a function which takes a single argument, also a function. That 11 | argument gets called with `X` and `Y`. That means to pull `X` back out, we 12 | just need to call `cons X Y` with a function like `? a | ? b $a`. Likewise for 13 | `Y`. That is exactly what `car` and `cdr` do. 14 | 15 | Definitions like that can be a bit hard to think about. But the great 16 | thing is that you can apply definitions like these without initially 17 | understanding them. So if the listener wants to try them out, they 18 | can; there's an element of interactivity beyond what a plain text 19 | message could give. 20 | -------------------------------------------------------------------------------- /msg/COS_Cons.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowTheLesson { 7 | my $txt = ""; 8 | 9 | $txt .= "intro cons;\n"; 10 | $txt .= "intro car;\n"; 11 | $txt .= "intro cdr;\n"; 12 | $txt .= "define cons | ? x | ? y | ? z | z \$x \$y;\n"; 13 | $txt .= "define car | ? z | z | ? x | ? y \$x;\n"; 14 | $txt .= "define cdr | ? z | z | ? x | ? y \$y;\n"; 15 | 16 | { 17 | my $idx = 0; 18 | my @examples = prand(10,10); 19 | for (my $i=0; $i<3; $i++) { 20 | my $x1 = $examples[$idx]; $idx++; 21 | my $x2 = $examples[$idx]; $idx++; 22 | $txt .= "assign x (cons $x1 $x2) | = $x1 | car \$x;\n"; 23 | $txt .= "assign x (cons $x1 $x2) | = $x2 | cdr \$x;\n"; 24 | } 25 | } 26 | { 27 | my $idx = 0; 28 | my @examples = prand(20,20); 29 | for (my $i=0; $i<3; $i++) { 30 | my $x1 = $examples[$idx]; $idx++; 31 | my $x2 = $examples[$idx]; $idx++; 32 | my $x3 = $examples[$idx]; $idx++; 33 | $txt .= "assign x (cons $x1 | cons $x2 $x3) | = $x1 | car \$x;\n"; 34 | $txt .= "assign x (cons $x1 | cons $x2 $x3) | = $x2 | car | cdr \$x;\n"; 35 | $txt .= "assign x (cons $x1 | cons $x2 $x3) | = $x3 | cdr | cdr \$x;\n"; 36 | } 37 | { 38 | my @examples = prand(5,5); 39 | my @pre = @examples[0..($#examples-1)]; 40 | my $post = $examples[$#examples]; 41 | for (my $k=0; $k<=$#examples; $k++) { 42 | $txt .= "assign x (cons " . join(" | cons ",@pre) . " $post) "; 43 | $txt .= "| = $examples[$k]"; 44 | if ($k<$#examples) { 45 | $txt .= " | car"; 46 | } 47 | for (my $i=0; $i<$k; $i++) { 48 | $txt .= " | cdr"; 49 | } 50 | $txt .= " \$x;\n"; 51 | } 52 | } 53 | } 54 | 55 | return $txt; 56 | } 57 | 58 | 59 | ShowLesson(ShowTheLesson()); 60 | 61 | -------------------------------------------------------------------------------- /msg/COS_D.gate: -------------------------------------------------------------------------------- 1 | 12 1 1 0 2 | 14 1 1 0 3 | 1 2 0 1 DATA 4 | 3 2 0 1 5 | 5 2 0 1 6 | 7 2 0 1 7 | 9 2 0 1 8 | 11 2 0 1 9 | 12 3 1 0 10 | 14 3 1 0 11 | 11 4 0 1 12 | 13 4 0 1 13 | 10 5 -1 0 14 | 14 5 1 0 15 | 1 6 0 1 CLK 16 | 3 6 0 1 17 | 5 6 0 1 18 | 7 6 0 1 19 | 9 6 0 1 20 | 11 6 0 1 21 | 12 7 1 0 22 | 14 7 1 0 23 | 11 8 0 1 24 | 13 8 0 1 25 | 10 9 -1 0 26 | 14 9 1 0 27 | 11 10 0 -1 28 | 13 10 0 -1 29 | 15 10 0 -1 30 | 12 11 1 0 31 | 13 12 0 1 32 | 15 12 0 1 33 | 17 12 0 1 34 | 19 12 0 1 35 | 36 | -------------------------------------------------------------------------------- /msg/COS_DefineFunction.js: -------------------------------------------------------------------------------- 1 | var cos = require("./cosmic"); 2 | cos.language(2); 3 | cos.seed(42); 4 | 5 | cos.add("define meaning-of-life-universe-everything 39"); 6 | cos.add("= 39 $meaning-of-life-universe-everything"); 7 | cos.add("= $meaning-of-life-universe-everything 39"); 8 | cos.add("= 49 | + 10 $meaning-of-life-universe-everything"); 9 | cos.add("define meaning-of-life-universe-everything 40"); 10 | cos.add("= $meaning-of-life-universe-everything 40"); 11 | cos.add("= 80 | * $meaning-of-life-universe-everything 2"); 12 | cos.add("define meaning-of-life-universe-everything | + 1 $meaning-of-life-universe-everything"); 13 | cos.add("= $meaning-of-life-universe-everything 41"); 14 | cos.add("assign x (+ 1 $meaning-of-life-universe-everything) | define meaning-of-life-universe-everything $x") 15 | cos.add("= $meaning-of-life-universe-everything 42"); 16 | 17 | cos.doc("Now we can start defining and naming functions. Here's one to square an integer."); 18 | cos.intro("square"); 19 | cos.add("define square | ? x | * $x $x"); 20 | 21 | var bag = cos.bag(0,10); 22 | for (var i=0; i<5; i++) { 23 | var x = bag[i]; 24 | cos.add(["=", x*x, [-1, "square", x]]); 25 | } 26 | 27 | cos.doc("Here's a function to increment an integer."); 28 | cos.intro("++"); 29 | cos.add("define ++ | ? x | + $x 1"); 30 | for (var i=0; i<5; i++) { 31 | var x = bag[i]; 32 | cos.add(["=", x+1, [-1, "++", x]]); 33 | } 34 | -------------------------------------------------------------------------------- /msg/COS_DefineFunction.md: -------------------------------------------------------------------------------- 1 | # MATH demonstrate existence of memory 2 | 3 | We've set up a way to name a value within an expression. Now let's go beyond that, 4 | and introduce a way to name a value in one sentence and use it in a later sentence. 5 | In other words, a message-level memory. After this, we'll be able to define new 6 | symbols from existing ones, with less need for large numbers of examples. 7 | 8 | We introduce a `define` symbol that works just like `assign`, except that it applies 9 | to the rest of the message rather than the rest of the sentence. 10 | 11 | A sentence of the form `define X Y` means that `$X` will evaluate to `Y` from that 12 | point on (unless `X` is changed by another `define`). 13 | 14 | The `meaning-of-life-universe-everything` symbol here is entirely arbitrary, and 15 | won't be encoded as anything particularly meaningful in the message. 16 | -------------------------------------------------------------------------------- /msg/COS_Demo.scm: -------------------------------------------------------------------------------- 1 | 2 | # PLAY test out message translation mechanism 3 | # tmp 4 | # tmp 5 | 6 | (+ 2 3); 7 | (demo | + 2 | + (+ 1 3) 4); 8 | 9 | (demo | primer); 10 | 11 | -------------------------------------------------------------------------------- /msg/COS_Dot.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.intro("."); 6 | 7 | cos.add(`define dotify:1 | ? pre | ? act | ? rem | 8 | if (= 0 | list-length $rem) (append $act $pre) | 9 | assign next (head $rem) | 10 | if (not | = . $next) (dotify:1 (append $act $pre) $next (tail $rem)) | 11 | dotify:1 $pre (vector $act | head | tail $rem) (tail | tail $rem)`); 12 | 13 | cos.add(`define dotify | ? lst | 14 | dotify:1 (vector) (head $lst) (tail $lst)`); 15 | 16 | cos.add(`list= (dotify | vector 1 2 . 3 4) (vector 1 (vector 2 3) 4)`); 17 | cos.add(`list= (dotify | vector 1 2 . 3 . 4 5) (vector 1 (vector (vector 2 3) 4) 5)`); 18 | 19 | cos.add(`define translate | assign prev $translate | ? x | 20 | if (not | function? $x) (prev $x) | 21 | if (<= (list-length $x) 1) (prev $x) | 22 | prev | dotify $x`); 23 | 24 | cos.add(`= + . 5 . 5 10`); 25 | cos.add(`= + . (- + . 4 . 4 3) . 5 * . 5 . 2`); 26 | -------------------------------------------------------------------------------- /msg/COS_Double.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowDoubleLesson { 7 | my $txt = ""; 8 | $txt .= "# MATH introduce doubling as a special case of multiplication\n"; 9 | $txt .= "# as prelude to binary representation\n"; 10 | $txt .= ShowLine(Op("intro",":")); 11 | for (my $i=0; $i<=4; $i++) 12 | { 13 | $txt .= ShowLine(Op2("=",Op1(":",ShowUnary($i)),ShowUnary($i*2))); 14 | } 15 | for (my $i=0; $i<=4; $i++) 16 | { 17 | $txt .= ShowLine(Op2("=",ShowUnary($i*2),Op1(":",ShowUnary($i)))); 18 | } 19 | for (my $i=0; $i<=4; $i++) 20 | { 21 | $txt .= ShowLine(Op2("=", 22 | Op2("*",ShowUnary($i),ShowUnary(2)), 23 | Op1(":",ShowUnary($i)))); 24 | } 25 | for (my $i=0; $i<=4; $i++) 26 | { 27 | $txt .= ShowLine(Op2("=", 28 | BareOp1(":",ShowUnary($i)), 29 | Op1(":",ShowUnary($i)))); 30 | } 31 | return $txt; 32 | }; 33 | 34 | 35 | ShowLesson(ShowDoubleLesson()); 36 | 37 | -------------------------------------------------------------------------------- /msg/COS_Element.md: -------------------------------------------------------------------------------- 1 | # PHYSICS introduce the elements 2 | 3 | Introducing the elements is done well in the [DearET message](https://web.archive.org/web/20161017192136/http://www.dearet.org/testing-a-message) by [Michael Busch](https://seti.org/our-scientists/michael-busch). We draw inspiration from that work. 4 | 5 | The general idea here is that there are some physical ratios that should be known by 6 | anybody with a handle on what is going on at the atomic level, and may be recognizable. 7 | The pattern of how elementary particles are combined into atoms, and atoms into 8 | molecules, may also ring a bell. 9 | -------------------------------------------------------------------------------- /msg/COS_Evaluation.md: -------------------------------------------------------------------------------- 1 | # MATH show alternative groupings 2 | 3 | We've advanced enough that we're starting to have choices about how 4 | something is expressed. Let's pause to acknowledge that, and 5 | reinforce some syntactic equivalences so the listener can be confident 6 | of them. 7 | 8 | Parentheses play a role in grouping expressions, just like in regular 9 | math. To reduce the mental burden of tracking nesting, we use a `|` shortcut 10 | that means "add parentheses between this point and as far as you can go." So for 11 | example `(= 7 | + 3 4)` is equivalent to `(= 7 (+ 3 4))`. 12 | -------------------------------------------------------------------------------- /msg/COS_Geo.scm: -------------------------------------------------------------------------------- 1 | 2 | # MUD another simple little text-adventure space 3 | 4 | # let us try to make a slightly more interesting world 5 | 6 | (define make-table | ? lst | 7 | reduce 8 | (? x | ? h | 9 | assign name (car $x) | 10 | assign obj (cdr $x) | 11 | hash-add $h $name $obj) 12 | (append $hash-null $lst)); 13 | 14 | # note, the quoted strings below are just represented as a big number, 15 | # nothing special 16 | (define geo-map | make-table | map 17 | (? name | cons $name | room new $name) 18 | (vector "boston" "dublin" "paris" "genoa")); 19 | 20 | (define my-links | map 21 | (? entry | 22 | assign src (car $entry) | 23 | assign dest (cdr $entry) | 24 | door new (geo-map $src) (geo-map $dest)) 25 | (vector 26 | (cons "boston" "dublin") 27 | (cons "dublin" "paris") 28 | (cons "boston" "paris") 29 | (cons "paris" "genoa"))); 30 | 31 | (define myrobo | robo new); 32 | 33 | (myrobo set-room | geo-map "dublin"); 34 | 35 | (demo | myrobo get-room name); 36 | 37 | (myrobo update); 38 | 39 | (demo | myrobo get-room name); 40 | 41 | (myrobo update); 42 | 43 | (demo | myrobo get-room name); 44 | 45 | (myrobo update); 46 | 47 | (demo | myrobo get-room name); 48 | 49 | (myrobo update); 50 | 51 | (demo | myrobo get-room name); 52 | 53 | (myrobo update); 54 | 55 | (demo | myrobo get-room name); 56 | 57 | (myrobo update); 58 | 59 | (demo | myrobo get-room name); 60 | 61 | 62 | # all characters should update together 63 | 64 | (class world (the-places the-links) 65 | (field things | container new) 66 | (field names | cell new $hash-null) 67 | (field places | cell new 0) 68 | (field links | cell new 0) 69 | (method new | begin 70 | (places set | make-table | map 71 | (? name | cons $name | room new $name) 72 | $the-places) 73 | (links set | map 74 | (? entry | 75 | assign src (car $entry) | 76 | assign dest (cdr $entry) | 77 | door new (places get $src) (places get $dest)) 78 | $the-links)) 79 | (method add | lambda (place name val) | begin 80 | (val set-room | places get $place) 81 | (val set-name $name) 82 | (names set | hash-add (names get) $name $val) 83 | (things add $val)) 84 | (method find | ? n | names get $n get-room name) 85 | (method reachable | ? place | 86 | assign exits (select-match (instanceof door) | places get $place inventory) | 87 | map (? door | door access-from (places get $place) name) $exits) 88 | (method update | begin 89 | (map (? x | x update) | things inventory) 90 | $true)); 91 | 92 | (define geo-world | world new 93 | (vector "boston" "dublin" "paris" "genoa") 94 | (vector 95 | (cons "boston" "dublin") 96 | (cons "dublin" "paris") 97 | (cons "boston" "paris") 98 | (cons "paris" "genoa"))); 99 | 100 | (geo-world add "dublin" "robo1" | robo new); 101 | 102 | (geo-world add "genoa" "robo2" | robo new); 103 | 104 | (demo | geo-world find "robo1"); 105 | (demo | geo-world find "robo2"); 106 | 107 | (geo-world update); 108 | 109 | (demo | geo-world find "robo1"); 110 | (demo | geo-world find "robo2"); 111 | 112 | (demo | geo-world reachable "boston"); 113 | 114 | (demo | geo-world reachable "genoa"); 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /msg/COS_Graph.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.seed(42); 6 | 7 | cos.intro("graph:make"); 8 | cos.add(`define graph:make | lambda (x:graph:1 x:graph:2) | pair $x:graph:1 $x:graph:2`); 9 | 10 | cos.add(`define demo:graph | graph:make 11 | (vector 1 2 3 4) 12 | (vector (pair 1 2) (pair 2 3) (pair 1 4))`); 13 | 14 | cos.intro("exists:graph:2"); 15 | cos.add(`define exists:graph:2 | lambda (x:graph y:graph:1 z:graph:1) | 16 | exists | ? n | 17 | if (or (< $n 0) (>= $n | list-length | list-ref $x:graph 1)) $false | 18 | list= (list-ref (list-ref $x:graph 1) $n) (pair $y:graph:1 $z:graph:1)`); 19 | 20 | cos.add(`exists:graph:2 $demo:graph 1 2`); 21 | cos.add(`not | exists:graph:2 $demo:graph 1 3`); 22 | cos.add(`not | exists:graph:2 $demo:graph 2 4`); 23 | cos.add(`exists:graph:2 $demo:graph 1 4`); 24 | 25 | cos.intro("exists:graph:2:list"); 26 | cos.add(`define exists:graph:2:list | lambda (x:graph y:graph:1 z:graph:1) | 27 | if (= $y:graph:1 $z:graph:1) $true | 28 | if (exists:graph:2 $x:graph $y:graph:1 $z:graph:1) $true | 29 | exists | ? n:graph:1 | 30 | if (not | exists:graph:2 $x:graph $y:graph:1 $n:graph:1) $false | 31 | exists:graph:2:list $x:graph $n:graph:1 $z:graph:1`); 32 | 33 | cos.add(`exists:graph:2:list $demo:graph 1 2`); 34 | cos.add(`exists:graph:2:list $demo:graph 1 3`); 35 | cos.add(`not | exists:graph:2:list $demo:graph 2 4`); 36 | -------------------------------------------------------------------------------- /msg/COS_Graph.md: -------------------------------------------------------------------------------- 1 | # MATH introduce graph structures 2 | 3 | -------------------------------------------------------------------------------- /msg/COS_Hash.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowHashLesson { 7 | my $txt = ""; 8 | $txt .= "# MATH introduce environment/hashmap structure\n"; 9 | $txt .= "# this section needs a LOT more examples :-)\n"; 10 | $txt .= "# note that at the time of writing (h 1 2) is same as ((h) 1 2)\n"; 11 | $txt .= "intro hash-add;\n"; 12 | $txt .= ShowLine(Op2("define", 13 | "hash-add", 14 | ProcMultiple(["x:hash","x","y","z"], 15 | Op("if", 16 | Op2("equal",Ref("z"),Ref("x")), 17 | Ref("y"), 18 | Op("x:hash", Ref("z")))))); 19 | $txt .= "intro hash-ref;\n"; 20 | $txt .= ShowLine(Op2("define", 21 | "hash-ref", 22 | ProcMultiple(["x:hash","x"], 23 | Op("x:hash", Ref("x"))))); 24 | 25 | $txt .= "intro hash-null;\n"; 26 | $txt .= ShowLine(Op2("define", 27 | "hash-null", 28 | Proc("x",Ref("undefined")))); 29 | 30 | $txt .= "intro hash-default;\n"; 31 | $txt .= ShowLine(Op2("define", 32 | "hash-default", 33 | Proc("default", 34 | Proc("x",Ref("default"))))); 35 | 36 | $txt .= ShowLine(Op2("define", 37 | "demo:hash", 38 | Op("hash-add", 39 | Op("hash-add", 40 | Ref("hash-null"), 41 | 3, 2), 42 | 4, 43 | 9))); 44 | 45 | $txt .= ShowLine(Op2("=", 46 | Op("hash-ref", 47 | Ref("demo:hash"), 48 | 4), 49 | 9)); 50 | 51 | $txt .= ShowLine(Op2("=", 52 | Op("hash-ref", 53 | Ref("demo:hash"), 54 | 3), 55 | 2)); 56 | 57 | $txt .= ShowLine(Op2("=", 58 | Op("hash-ref", 59 | Ref("demo:hash"), 60 | 8), 61 | Ref("undefined"))); 62 | 63 | $txt .= ShowLine(Op2("=", 64 | Op("hash-ref", 65 | Ref("demo:hash"), 66 | 15), 67 | Ref("undefined"))); 68 | 69 | $txt .= ShowLine(Op2("=", 70 | Op("hash-ref", 71 | Op("hash-add", 72 | Ref("demo:hash"), 73 | 15, 74 | 33), 75 | 15), 76 | 33)); 77 | 78 | $txt .= ShowLine(Op2("=", 79 | Op("hash-ref", 80 | Ref("demo:hash"), 81 | 15), 82 | Ref("undefined"))); 83 | 84 | $txt .= "intro make-hash;\n"; 85 | $txt .= ShowLine(Op2("define", 86 | "make-hash", 87 | Proc("x", 88 | Op("if", 89 | Op2("list=",Ref("x"),Op("vector")), 90 | Ref("hash-null"), 91 | Op("hash-add", 92 | Op("make-hash", Op1("tail", Ref("x"))), 93 | Op1("first",Op1("head",Ref("x"))), 94 | Op1("second",Op1("head",Ref("x")))))))); 95 | 96 | $txt .= ShowLine(Op2("=", 97 | Op2("hash-ref", 98 | Op1("make-hash", 99 | Op("vector", 100 | Op2("pair", 3, 10), 101 | Op2("pair", 2, 20), 102 | Op2("pair", 1, 30))), 103 | 3), 104 | 10)); 105 | $txt .= ShowLine(Op2("=", 106 | Op2("hash-ref", 107 | Op1("make-hash", 108 | Op("vector", 109 | Op2("pair", 3, 10), 110 | Op2("pair", 2, 20), 111 | Op2("pair", 1, 30))), 112 | 1), 113 | 30)); 114 | 115 | return $txt; 116 | }; 117 | 118 | 119 | ShowLesson(ShowHashLesson()); 120 | 121 | -------------------------------------------------------------------------------- /msg/COS_If.md: -------------------------------------------------------------------------------- 1 | # MATH show conditionals 2 | 3 | Now that we spent some time looking at `true` and `false`, let's show 4 | a way to build conditional expressions. We start with an `if` expression, 5 | of the form `if CONDITION E1 E2`, which evaluates to `E1` if the `CONDITION` 6 | is `true`, otherwise `E2`. 7 | 8 | If the listener is trying to map the language we are describing onto 9 | their own system of computation, it is pretty important that `if` be 10 | "lazy," and completely skip evaluating the branch not taken. That 11 | should become clear fairly soon if they were to try an "eager" `if`. 12 | -------------------------------------------------------------------------------- /msg/COS_If.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowIfLesson { 7 | 8 | my $txt = ""; 9 | 10 | $txt .= ShowLine(Op("intro","if")); 11 | 12 | for (my $i=0; $i<8; $i++) 13 | { 14 | my $r1 = irand(2); 15 | my $r2 = 20+irand(10); 16 | my $r3 = 20+irand(10); 17 | my $cmp = ShowTrue(); 18 | my $out = ""; 19 | if ($r1) 20 | { 21 | $cmp = ShowTrueComparisonBin(); 22 | } 23 | else 24 | { 25 | $cmp = ShowFalseComparisonBin(); 26 | } 27 | if ($r1) 28 | { 29 | $out .= Num($r2); 30 | } 31 | else 32 | { 33 | $out = Num($r3); 34 | } 35 | $txt .= ShowLine(TailOp2("=", 36 | $out, 37 | Op("if", 38 | $cmp, 39 | Num($r2), 40 | Num($r3)))); 41 | } 42 | 43 | $txt .= "~ We can now define more interesting functions. Here's the maximum of two integers:\n"; 44 | $txt .= "(intro max);\n"; 45 | $txt .= "(define max | ? x | ? y | if (> \$x \$y) \$x \$y);\n"; 46 | for (my $r1=0; $r1<3; $r1++) { 47 | for (my $r2=0; $r2<3; $r2++) { 48 | my $rmax = ($r1>$r2)?$r1:$r2; 49 | $txt .= "(= $rmax | max $r1 $r2);\n"; 50 | } 51 | } 52 | $txt .= "~ Now the minimum of two integers:\n"; 53 | $txt .= "(intro min);\n"; 54 | $txt .= "(define min | ? x | ? y | if (< \$x \$y) \$x \$y);\n"; 55 | for (my $r1=0; $r1<3; $r1++) { 56 | for (my $r2=0; $r2<3; $r2++) { 57 | my $rmin = ($r1<$r2)?$r1:$r2; 58 | $txt .= "(= $rmin | min $r1 $r2);\n"; 59 | } 60 | } 61 | $txt .= "~ Why should human CS students be the only ones the factorial example is inflicted on...\n"; 62 | $txt .= "(intro factorial);\n"; 63 | $txt .= "(define factorial | ? x | if (< \$x 1) 1 | * \$x | factorial | - \$x 1);\n"; 64 | my $v = 1; 65 | for (my $i=1; $i<=5; $i++) { 66 | $v = $v*$i; 67 | $txt .= "(= $v | factorial $i);\n"; 68 | } 69 | 70 | return $txt; 71 | }; 72 | 73 | 74 | ShowLesson(ShowIfLesson()); 75 | 76 | -------------------------------------------------------------------------------- /msg/COS_Implication.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowImplicationLesson { 7 | my $txt = ""; 8 | $txt .= "# MATH introduce logical implication\n"; 9 | $txt .= ShowLine(Op("intro","=>")); 10 | $txt .= ShowLine(Op("define", 11 | "=>", 12 | Proc("x", 13 | Proc("y", 14 | Op1("not", 15 | Op2("and", 16 | Ref("x"), 17 | Op1("not", 18 | Ref("y")))))))); 19 | $txt .= ShowLine(Op2("=>",ShowTrue(),ShowTrue())); 20 | $txt .= ShowLine(Op1("not",Op2("=>",ShowTrue(),ShowFalse()))); 21 | $txt .= ShowLine(Op2("=>",ShowFalse(),ShowTrue())); 22 | $txt .= ShowLine(Op2("=>",ShowFalse(),ShowFalse())); 23 | $txt .= ShowLine(Op("forall", 24 | Proc(Lit("x"), 25 | Op("forall", 26 | Proc(Lit("y"), 27 | Op2("=>", 28 | Op2("=>", 29 | Ref("x"), 30 | Ref("y")), 31 | Op2("=>", 32 | Op1("not",Ref("y")), 33 | Op1("not",Ref("x"))))))))); 34 | return $txt; 35 | }; 36 | 37 | 38 | ShowLesson(ShowImplicationLesson()); 39 | 40 | -------------------------------------------------------------------------------- /msg/COS_Intro.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var cos = require("./cosmic"); 4 | 5 | cos.language(2); 6 | 7 | cos.add("intro unary"); 8 | cos.add("intro is:int"); 9 | for (var i=0; i<16; i++) { 10 | var ones = ""; 11 | for (var j=0; j-V (self)) 7 | (method (self -V)) 8 | (method hasNext-Z (> (list-length | pipe get) 0)) 9 | (method hasNext (self hasNext-Z)) 10 | (method next (self next-Object)) 11 | (method next-Object 12 | (let ((result (head | pipe get))) 13 | (begin 14 | (pipe set | tail | pipe get) 15 | (result))))); 16 | 17 | (define test1 (COS_JList new)); 18 | 19 | (begin 20 | (test1 add 15) 21 | (test1 add 72) 22 | (test1 add 99) 23 | (true)); 24 | 25 | (define iter1 (test1 iterator)); 26 | 27 | (iter1 hasNext); 28 | (demo | iter1 next); 29 | (iter1 hasNext); 30 | (demo | iter1 next); 31 | (iter1 hasNext); 32 | (demo | iter1 next); 33 | (not | iter1 hasNext); 34 | 35 | -------------------------------------------------------------------------------- /msg/COS_JList_impl.scm: -------------------------------------------------------------------------------- 1 | # JAVA native implementation of a Java list, hash classes 2 | 3 | (define flex-equals 4 | (lambda (x y) 5 | (if (not | function? | x) 6 | (if (not | function? | y) 7 | (= (x) (y)) 8 | (false)) 9 | (if (not | function? | y) 10 | (false) 11 | (x equals (y)))))); 12 | 13 | (define remove-object 14 | (lambda (x) 15 | (remove-match (lambda (y) 16 | (flex-equals (x) (y)))))); 17 | 18 | (define contains-object 19 | (lambda (x lst) 20 | (if (> (list-length | lst) 0) 21 | (if (flex-equals (head | lst) (x)) 22 | (true) 23 | (contains-object (x) (tail | lst))) 24 | (false)))); 25 | 26 | (class COS_JList () 27 | (field super ((java lang Object) new)) 28 | (method unknown (lambda (x) (super (x)))) 29 | (field contents (cell new (vector))) 30 | (method -V (self)) 31 | (method (self -V)) 32 | (method add-Object-V (lambda (x) 33 | (contents set (prepend (x) (contents get))))) 34 | (method add (self add-Object-V)) 35 | (method remove-Object-Z (lambda (x) 36 | (contents set 37 | (remove-object (x) (contents get))))) 38 | (method remove (self remove-Object-Z)) 39 | (method contains-Object-Z (lambda (x) 40 | (contains-object (x) (contents get)))) 41 | (method contains (self contains-Object-Z)) 42 | (method get-I-Object (lambda (x) 43 | (list-ref (contents get) (x)))) 44 | (method get (self get-I-Object)) 45 | (method iterator-Iterator (COS_JListIterator new (self))) 46 | (method iterator (self iterator-Iterator)) 47 | (method size-V-I (list-length (contents get))) 48 | (method size (self size-V-I))); 49 | 50 | 51 | (define test1 (COS_JList new)); 52 | 53 | (begin (test1 add-Object-V (test1)) 54 | (= 1 | test1 size-V-I)); 55 | 56 | (test1 == (test1 get-I-Object 0)); 57 | 58 | (class COS_JHashMap () 59 | (field super ((java lang Object) new)) 60 | (method unknown (lambda (x) (super (x)))) 61 | (field contents (cell new (? x 0))) 62 | (method -V (self)) 63 | (method (self -V)) 64 | (method put-Object-Object-V (lambda (x y) 65 | (let ((prev | contents get)) 66 | (contents set 67 | (? z 68 | (if (flex-equals (z) (x)) 69 | (y) 70 | (prev (z)))))))) 71 | (method put (self put-Object-Object-V)) 72 | (method get-Object-Object (lambda (x) 73 | (contents get (x)))) 74 | (method get (self get-Object-Object))); 75 | 76 | (define test2 (COS_JHashMap new)); 77 | 78 | (begin (test2 put-Object-Object-V 5 10) 79 | (= 10 | test2 get 5)); 80 | 81 | -------------------------------------------------------------------------------- /msg/COS_JList_test.scm: -------------------------------------------------------------------------------- 1 | 2 | # JAVA testing the JList class 3 | 4 | (define test1 (COS_JList new)); 5 | 6 | (begin (test1 add-Object-V (test1)) 7 | (= 1 (test1 size-V-I))); 8 | 9 | ((test1 get-I-Object 0) == (test1)); 10 | 11 | 12 | -------------------------------------------------------------------------------- /msg/COS_JNamed.java: -------------------------------------------------------------------------------- 1 | 2 | public class COS_JNamed { 3 | private String name = "-"; 4 | private COS_JWorld world = null; 5 | 6 | void setName(String name) { 7 | this.name = name; 8 | } 9 | 10 | String getName() { 11 | return name; 12 | } 13 | 14 | void setWorld(COS_JWorld world) { 15 | this.world = world; 16 | } 17 | 18 | COS_JWorld getWorld() { 19 | return world; 20 | } 21 | 22 | void update() { 23 | } 24 | 25 | void postUpdate() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /msg/COS_JRobo.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Iterator; 3 | 4 | public class COS_JRobo extends COS_JThing { 5 | private COS_JHashMap times; 6 | private int now; 7 | 8 | public COS_JRobo() { 9 | times = new COS_JHashMap(); 10 | now = 1; 11 | } 12 | 13 | public void update() { 14 | COS_JRoom location = getRoom(); 15 | //System.out.println("Updating robo..."); 16 | if (location!=null) { 17 | int oldestTime = now; 18 | COS_JDoor oldestDoor = null; 19 | for (Iterator i = location.getDoors(); i.hasNext(); ) { 20 | COS_JDoor door = (COS_JDoor) i.next(); 21 | //System.out.println(" scanning door "); 22 | Integer t = (Integer)times.get(door); 23 | int v = 0; 24 | if (t!=null) { 25 | v = t.intValue(); 26 | } 27 | if (v " + getName()); 26 | doors.add(door); 27 | } 28 | 29 | public void addThing(COS_JThing thing) { 30 | content.add(thing); 31 | } 32 | 33 | public void removeThing(COS_JThing thing) { 34 | content.remove(thing); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /msg/COS_JRoom_test.scm: -------------------------------------------------------------------------------- 1 | 2 | # JAVA test JRoom, JDoor, JThing, etc 3 | 4 | (define s (? x | String new int-init | x)); 5 | 6 | (define room1 (COS_JRoom new )); 7 | (define room2 (COS_JRoom new )); 8 | (define door12 (COS_JDoor new 9 | (room1) (s "south") (room2) (s "north"))); 10 | (define jworld (COS_JWorld new )); 11 | 12 | (define thing1 (COS_JThing new )); 13 | (define robo1 (COS_JRobo new )); 14 | 15 | (act | jworld add (thing1) | s "bus"); 16 | (act | jworld add (robo1) | s "autobus"); 17 | (act | jworld add (room1) | s "boston"); 18 | (act | jworld add (room2) | s "newyork"); 19 | 20 | (begin (room1 get add (room1)) 21 | (= 1 | room1 get size)); 22 | 23 | (= 1 | room1 get size); 24 | 25 | (= 0 | room2 get size); 26 | 27 | (act | thing1 setRoom (room1)); 28 | 29 | (= 2 | room1 get size); 30 | 31 | (= 0 | room2 get size); 32 | 33 | (act | thing1 setRoom (room2)); 34 | 35 | (room1 get size); 36 | 37 | (room2 get size); 38 | 39 | (thing1 equals (thing1)); 40 | (room1 equals (room1)); 41 | (not | thing1 equals (room1)); 42 | 43 | (demo | door12 apply (room1) (s "south") getName intValue); 44 | (demo | door12 apply (room2) (s "north") getName intValue); 45 | 46 | (define o 47 | (? x | jworld get | s | x)); 48 | 49 | (= "newyork" | (o "bus") getRoom getName intValue); 50 | 51 | (act | robo1 setRoom (room1)); 52 | 53 | (demo | (o "autobus") getRoom getName intValue); 54 | (act | jworld update); 55 | (demo | (o "autobus") getRoom getName intValue); 56 | 57 | -------------------------------------------------------------------------------- /msg/COS_JThing.java: -------------------------------------------------------------------------------- 1 | 2 | public class COS_JThing extends COS_JNamed { 3 | private COS_JRoom location; 4 | private COS_JRoom nextLocation; 5 | 6 | public void setRoom(COS_JRoom location) { 7 | if (this.location!=null) { 8 | this.location.removeThing(this); 9 | } 10 | this.location = location; 11 | location.addThing(this); 12 | this.nextLocation = location; 13 | } 14 | 15 | public COS_JRoom getRoom() { 16 | return location; 17 | } 18 | 19 | public void setNextRoom(COS_JRoom location) { 20 | nextLocation = location; 21 | } 22 | 23 | public void postUpdate() { 24 | if (nextLocation!=location) { 25 | setRoom(nextLocation); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /msg/COS_JWorld.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Iterator; 3 | 4 | public class COS_JWorld { 5 | private COS_JHashMap content; 6 | private COS_JList inventory; 7 | 8 | public COS_JWorld() { 9 | content = new COS_JHashMap(); 10 | inventory = new COS_JList(); 11 | } 12 | 13 | public void add(COS_JNamed named, String name) { 14 | named.setName(name); 15 | content.put(named.getName(),named); 16 | inventory.add(named); 17 | } 18 | 19 | public COS_JNamed get(String name) { 20 | return (COS_JNamed)content.get(new String(name)); 21 | } 22 | 23 | public void update() { 24 | for (Iterator i = inventory.iterator(); i.hasNext(); ) { 25 | COS_JNamed o = (COS_JNamed) i.next(); 26 | o.update(); 27 | } 28 | for (Iterator i = inventory.iterator(); i.hasNext(); ) { 29 | COS_JNamed o = (COS_JNamed) i.next(); 30 | o.postUpdate(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /msg/COS_JavaMain.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.IOException; 3 | 4 | // This code is not intended to go into the message. 5 | // It can be used to exercise parts of the message that are in Java. 6 | 7 | class COS_JavaMain { 8 | public void testJList() { 9 | System.out.println("JList..."); 10 | COS_JList lst = new COS_JList(); 11 | lst.add(new Integer(11)); 12 | lst.add(new Integer(22)); 13 | lst.add(new Integer(33)); 14 | lst.remove(new Integer(22)); 15 | assert (lst.contains(new Integer(11))) : "bad containment"; 16 | assert (!lst.contains(new Integer(22))) : "bad removal"; 17 | assert (lst.size()==2) : "wrong list size"; 18 | System.out.println("...passed"); 19 | } 20 | 21 | public void testJHashMap() { 22 | System.out.println("JHashMap..."); 23 | COS_JHashMap map = new COS_JHashMap(); 24 | map.put(new Integer(11), new Integer(77)); 25 | assert (((Integer)map.get(new Integer(11))).intValue()==77) : "wrong get value"; 26 | System.out.println("...passed"); 27 | } 28 | 29 | public void testJWorld() { 30 | System.out.println("JWorld..."); 31 | COS_JWorld world = new COS_JWorld(); 32 | COS_JRoom boston = new COS_JRoom(); 33 | COS_JRoom newyork = new COS_JRoom(); 34 | COS_JRobo bus = new COS_JRobo(); 35 | world.add(boston, "boston"); 36 | world.add(newyork, "newyork"); 37 | world.add(bus, "bus"); 38 | bus.setRoom(boston); 39 | new COS_JDoor((COS_JRoom)world.get("boston"), 40 | "south", 41 | (COS_JRoom)world.get("newyork"), 42 | "north"); 43 | assert(bus.getRoom().getName().equals("boston")); 44 | world.update(); 45 | assert(bus.getRoom().getName().equals("newyork")) : "wrong final dest"; 46 | System.out.println("...passed"); 47 | } 48 | 49 | public String getAct() { 50 | String txt = ""; 51 | boolean done = false; 52 | while (!done) { 53 | try { 54 | char ch = (char) System.in.read(); 55 | if (ch<0||ch=='\n') { 56 | done = true; 57 | } else { 58 | txt = txt + ch; 59 | } 60 | } catch (IOException e) { 61 | e.printStackTrace(); 62 | System.exit(0); 63 | } 64 | } 65 | System.out.println("act " + txt); 66 | return txt; 67 | } 68 | 69 | public void testRun() { 70 | System.out.println("Running..."); 71 | String cmd = getAct(); 72 | 73 | COS_JWorld world = new COS_JWorld(); 74 | COS_JRoom boston = new COS_JRoom(); 75 | COS_JRoom newyork = new COS_JRoom(); 76 | COS_JRobo bus = new COS_JRobo(); 77 | world.add(boston, "boston"); 78 | world.add(newyork, "newyork"); 79 | world.add(bus, "bus"); 80 | bus.setRoom(boston); 81 | 82 | while (1) { 83 | System.out.println("current location is ", 84 | bus.getRoom().getName()); 85 | Iterator it = bus.getRoom().getDoors(); 86 | String cmd = getAct(); 87 | //world.update(); 88 | } 89 | 90 | } 91 | 92 | public void testAll() { 93 | testJList(); 94 | testJHashMap(); 95 | testJWorld(); 96 | testRun(); 97 | } 98 | 99 | public static void main(String[] args) { 100 | System.out.println("Running tests..."); 101 | COS_JavaMain main = new COS_JavaMain(); 102 | main.testAll(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /msg/COS_JavaTest.java: -------------------------------------------------------------------------------- 1 | public class COS_JavaTest { 2 | private int q = 0; 3 | public int add(int x, int y) { 4 | return x + y; 5 | } 6 | public int sub(int x, int y) { 7 | return x - y; 8 | } 9 | public int mult(int x, int y) { 10 | return x * y; 11 | } 12 | public int addmult(int x, int y, int z) { 13 | return add(x, mult(y, z)); 14 | } 15 | public void set(int x) { 16 | q = x; 17 | } 18 | public int get() { 19 | return q; 20 | } 21 | public int fact(int x) { 22 | return ( x> 0) ? (x * fact(sub(x, 1))) : 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /msg/COS_JavaValidate.scm: -------------------------------------------------------------------------------- 1 | 2 | # JAVA check that automatic conversion is workable 3 | 4 | 5 | (define test1 | COS_JavaTest new); 6 | 7 | # Note that the names of methods include type information. 8 | # This could easily be removed, but is retained so that overloading 9 | # is possible in the Java code. 10 | # I is integer, V is void. The last type in the name is the return type. 11 | 12 | (= (test1 mult-I-I-I 15 10) 150); 13 | 14 | # The type information can be safely omitted if there is no ambiguity 15 | (= (test1 mult 15 10) 150); 16 | 17 | (= (test1 addmult-I-I-I-I 4 15 10) 154); 18 | 19 | (begin 20 | (test1 set-I-V 87) 21 | (= (test1 get-I) 87)); 22 | 23 | (= (test1 fact-I-I 0) 1); 24 | 25 | (= (test1 fact-I-I 1) 1); 26 | 27 | (= (test1 fact-I-I 5) 120); 28 | 29 | # Yay! testing says this works. 30 | # So structure for bytecode interpretation is in place. 31 | # Very few opcodes actually implemented yet though. 32 | 33 | 34 | -------------------------------------------------------------------------------- /msg/COS_Let.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.add("intro let"); 6 | cos.add("define translate:let:begin $translate"); 7 | cos.add(` 8 | define translate:let | ? x | ? y | 9 | if (= 0 | list-length $x) (translate $y) | 10 | translate:let (tail $x) | 11 | vector assign (head | head $x) (head | tail | head $x) $y`); 12 | cos.add(` 13 | define translate | ? x | 14 | if (not | function? $x) (translate:let:begin $x) | 15 | if (not | = let | head $x) (translate:let:begin $x) | 16 | translate:let (head | tail $x) (head | tail | tail $x)`); 17 | cos.add("let ((x 20)) | = $x 20"); 18 | cos.add("let ((x 50) (y 20)) | = 30 | - $x $y"); 19 | cos.add("= (let ((x 10)) | + $x 5) (assign x 10 | + $x 5)") 20 | cos.add("= (let ((x 10)) | + $x 5) ((? x | + $x 5) 10)") 21 | cos.add("= (let ((x 10) (y 5)) | + $x $y) (assign x 10 | assign y 5 | + $x $y)"); 22 | cos.add("= (let ((x 10) (y 5)) | + $x $y) ((? x | ? y | + $x $y) 10 5)"); 23 | -------------------------------------------------------------------------------- /msg/COS_Let.md: -------------------------------------------------------------------------------- 1 | # SYNTAX define let expressions 2 | 3 | Sometimes it is nice to do a lot of assignments at once. 4 | We introduce `let ((k1 v1) (k2 v2)) body` which is equivalent to 5 | `assign k1 v1 | assign k2 v2 | body`. 6 | -------------------------------------------------------------------------------- /msg/COS_License.scm: -------------------------------------------------------------------------------- 1 | # Author: Paul Fitzpatrick, paulfitz@alum.mit.edu 2 | # Copyright (c) 2018 Paul Fitzpatrick 3 | # 4 | # This file is part of CosmicOS. 5 | # 6 | # CosmicOS is free software; you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation; either version 2 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # CosmicOS is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with CosmicOS; if not, write to the Free Software 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | # 20 | 21 | -------------------------------------------------------------------------------- /msg/COS_List.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var cos = require("./cosmic"); 4 | cos.language(2); 5 | cos.seed(42); 6 | 7 | cos.comment("MATH some more list functions"); 8 | cos.intro("list:find"); 9 | cos.add(` 10 | define list:find:0 | ? x:list | ? y | ? n | 11 | if (= (list-length $x:list) 0) $undefined | 12 | if (equal (head $x:list) $y) $n | 13 | list:find:0 (tail $x:list) $y (+ $n 1)`); 14 | 15 | cos.add("define list:find | ? x:list | ? y | list:find:0 $x:list $y 0"); 16 | 17 | for (var i=0; i<10; i++) { 18 | var len = cos.irand(10)+1; 19 | var lst = []; 20 | for (var j=0; j= 1 | list-length $x) (vector) | 58 | prepend (head $x) | except-last | tail $x`); 59 | cos.add("= 15 | last | vector 4 5 15"); 60 | cos.add("list= (vector 4 5) | except-last | vector 4 5 15"); 61 | 62 | cos.add(`intro list:reverse`); 63 | cos.add(`define list:reverse | ? x:list | 64 | if (<= (list-length $x:list) 1) $x:list | 65 | prepend (last $x:list) | list:reverse | except-last $x:list`); 66 | 67 | cos.add(`list= (list:reverse | vector 1 2 3) (vector 3 2 1)`); 68 | cos.add(`list= (list:reverse | vector 50 1 33 99) (vector 99 33 1 50)`); 69 | 70 | cos.intro("append"); 71 | cos.add(`define append | ? x | ? lst | 72 | if (= 0 | list-length $lst) (vector $x) | 73 | prepend (head | $lst) | append $x | tail $lst`); 74 | 75 | cos.add(`list= (vector 1 2 5) | append 5 | vector 1 2`); 76 | 77 | cos.intro("select-match"); 78 | cos.add(`define select-match | ? test | ? lst | 79 | if (= 0 | list-length $lst) $lst | 80 | if (not | test | head $lst) (select-match $test | tail $lst) | 81 | prepend (head $lst) (select-match $test | tail $lst)`); 82 | 83 | cos.add(`list= (vector 14 19 13) | select-match (? x | > $x 10) | vector 1 14 19 3 13 0 4`); 84 | 85 | -------------------------------------------------------------------------------- /msg/COS_ListPre.md: -------------------------------------------------------------------------------- 1 | # MATH introduce lists 2 | 3 | Lists are a handy data structure to have. We'd like to get to the point 4 | in the message where we can make lists like this: `vector 1 4 5`, 5 | `vector 77 $undefined (vector 1 2 3) 14`, etc. But 6 | `vector` can't be a function in the language we've described up to now, 7 | it just can't work syntactically. 8 | What we can do is make lists like this: `(list 3) 1 4 5`, 9 | `(list 4) 77 $undefined ((list 3) 1 2 3) 14`, where we manually 10 | specify how many values are in the list. 11 | And then we can introduce a way to transform the syntax of the language, 12 | so that `vector 1 4 5` gets rewritten to `(list 3) 1 4 5` prior to being 13 | evaluated. 14 | 15 | An alternative would be just to introduce some special new syntax for 16 | lists, and give examples. If the listener finds our transformation approach 17 | confusing, they can simply ignore it and pick the message up again once 18 | `vector` is in place. But by giving the transformation, we offer a second 19 | way to understand and experiment with the concepts being introduced. 20 | -------------------------------------------------------------------------------- /msg/COS_MARK.scm: -------------------------------------------------------------------------------- 1 | 2 | # Message is pretty solid up to this point. 3 | # For testing purposes, useful to save state here to disk, 4 | # command: DISK-SAVE base 5 | 6 | -------------------------------------------------------------------------------- /msg/COS_Map.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require("./cosmic"); 4 | 5 | cos.language(2); 6 | cos.seed(42); 7 | 8 | cos.intro("map"); 9 | cos.add(` 10 | define map | ? x:? | ? x:list | 11 | if (= 0 | list-length $x:list) (list 0) | 12 | prepend (x:? | head $x:list) (map $x:? | tail $x:list)`); 13 | 14 | for (let i = 0; i < 4; i++) { 15 | const lst = cos.prand(20, i + 3); 16 | const out = lst.map(v => 2 * v); 17 | cos.add(`list= (${cos.listExpression(out)}) | map (? x | * $x 2) | ${cos.listExpression(lst)}`); 18 | } 19 | 20 | for (let i = 0; i < 4; i++) { 21 | const lst = cos.prand(20, i + 3); 22 | const out = lst.map(v => 42); 23 | cos.add(`list= (${cos.listExpression(out)}) | map (? x 42) | ${cos.listExpression(lst)}`); 24 | } 25 | 26 | cos.intro("reduce"); 27 | cos.add(` 28 | define reduce | ? x:? | ? x:list | 29 | if (= 0 | list-length $x:list) $undefined | 30 | if (= 1 | list-length $x:list) (head $x:list) | 31 | x:? (head $x:list) (reduce $x:? | tail $x:list)`); 32 | 33 | for (let i = 0; i < 4; i++) { 34 | const lst = cos.prand(20, i + 3); 35 | const out = lst.reduce((a, b) => a + b); 36 | cos.add(`= ${out} | reduce $+ | ${cos.listExpression(lst)}`); 37 | } 38 | -------------------------------------------------------------------------------- /msg/COS_Map.md: -------------------------------------------------------------------------------- 1 | # MATH introduce map and reduce 2 | 3 | For programming and for math, it is handy to be able to apply an element-wise 4 | transform to a list, and some kind of accumulator to pull out a summary. 5 | -------------------------------------------------------------------------------- /msg/COS_Message.scm: -------------------------------------------------------------------------------- 1 | 2 | # SELF a mechanism for referring to parts of the message 3 | 4 | # Many choices for how to do this. 5 | # Could do it without special machinery by using the 6 | # standard A-B trick for giving e.g. a Turing machine 7 | # access to its own description. 8 | # Instead, will simply introduce a "primer" function 9 | # that gives access to every statement made so far 10 | # (question: should future statements be included? 11 | # tentatively assume YES: will simplify 12 | # discussion of creating modified copies of the 13 | # complete message). 14 | 15 | # For now, assume primer is a list of statements, 16 | # with each statement being a list in the same 17 | # form as "translate" functions expect. 18 | # This means that there is, for now, no 19 | # distinction between unary or binary, 20 | # and the "|" structure is expanded. 21 | 22 | (intro primer); 23 | 24 | # this line is referred to later - change/move carefully 25 | (equal (list-ref $primer 0) | vector intro unary); 26 | (equal (list-ref $primer 1) | vector intro is:int); 27 | (equal (list-ref $primer 2) | vector is:int | vector unary 0); 28 | (equal (list-ref $primer 3) | vector is:int | vector unary 1 0); 29 | (assign idx (list:find $primer | vector intro primer) | 30 | equal (list-ref $primer | + $idx 1) | 31 | quote @@ | equal (list-ref $primer 0) | vector intro unary); 32 | 33 | # Now, we could return to the MUD, simulate an agent A 34 | # transferring a copy of the primer to another agent B, 35 | # and then show B making a modified copy of that primer 36 | # and passing it back to A. 37 | 38 | # We could also show agents experimenting with the 39 | # primer in various ways. 40 | -------------------------------------------------------------------------------- /msg/COS_MultipleParameter.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const cos = require('./cosmic'); 3 | 4 | cos.comment("SYNTAX lambda functions"); 5 | cos.add("intro lambda"); 6 | cos.add("define translate:lambda:begin $translate"); 7 | cos.add(` 8 | define translate | 9 | let ((x:translate $translate:lambda:begin)) | 10 | ? x | 11 | if (not | function? $x) (x:translate $x) | 12 | if (not | = lambda | head $x) (x:translate $x) | 13 | let ((x:list | head | tail $x) 14 | (y | head | tail | tail $x)) | 15 | if (= 0 | list-length $x:list) (translate $y) | 16 | translate | vector lambda (except-last $x:list) | 17 | vector ? (last $x:list) $y`); 18 | 19 | for (let i = 0; i < 5; i++) { 20 | const r2 = cos.irand(10); 21 | const r1 = cos.irand(10) + r2; 22 | cos.add(`= ${r1 - r2} | (lambda (x y) | - $x $y) ${r1} ${r2}`); 23 | } 24 | -------------------------------------------------------------------------------- /msg/COS_Multiplication.md: -------------------------------------------------------------------------------- 1 | # MATH introduce multiplication 2 | 3 | While we're at it, let's introduce multiplication with `= X | * Y Z` sentences. 4 | As for addition and subtraction, there will be some ambiguity as to whether we are 5 | presenting multiplication or division here, until syntax is clearly understood. 6 | -------------------------------------------------------------------------------- /msg/COS_Multiplication.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowMultiplicationLesson { 7 | my $txt = ""; 8 | $txt .= ShowLine(Op("intro","*")); 9 | for (my $i=0; $i<=3; $i++) 10 | { 11 | for (my $j=0; $j<=3; $j++) 12 | { 13 | $txt .= ShowLine(TailOp2("=", 14 | ShowUnary($i*$j), 15 | Op2("*", 16 | ShowUnary($i), 17 | ShowUnary($j)))); 18 | } 19 | } 20 | for (my $i=0; $i<10; $i++) 21 | { 22 | my $r = irand(4); 23 | my $r2 = irand(4); 24 | $txt .= ShowLine(TailOp2("=", 25 | ShowUnary($r*$r2), 26 | Op2("*", 27 | ShowUnary($r), 28 | ShowUnary($r2)))); 29 | } 30 | return $txt; 31 | }; 32 | 33 | 34 | ShowLesson(ShowMultiplicationLesson()); 35 | 36 | -------------------------------------------------------------------------------- /msg/COS_Mutable.md: -------------------------------------------------------------------------------- 1 | # MATH introduce mutable cells 2 | 3 | With `define`, we showed that there is a global memory, where we can associate a 4 | symbol with a value. That's nice, but it can be handy to separate memory from 5 | naming. In this section we introduce `make-cell X`, which creates a "cell" 6 | of memory and puts `X` in it. The cell can be read with `get!`, like 7 | `get! | make-cell X`, or written to with `set!`, like `set! (make-cell-X) Y`. 8 | -------------------------------------------------------------------------------- /msg/COS_Mutable.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowMutableLesson { 7 | my $txt = ""; 8 | $txt .= ShowLine(Op("intro","make-cell")); 9 | $txt .= ShowLine(Op("intro","set!")); 10 | $txt .= ShowLine(Op("intro","get!")); 11 | $txt .= ShowLine(Op("define", 12 | "demo:make-cell:x", 13 | TailOp("make-cell", 14))); 14 | $txt .= "= (get! \$demo:make-cell:x) 14;\n"; 15 | $txt .= ShowLine(Op2("set!", 16 | Ref("demo:make-cell:x"), 17 | 15)); 18 | $txt .= ShowLine(Op2("=", 19 | Op1("get!", Ref("demo:make-cell:x")), 20 | 15)); 21 | $txt .= ShowLine(Op2("set!", 22 | Ref("demo:make-cell:x"), 23 | 5)); 24 | $txt .= ShowLine(Op2("set!", 25 | Ref("demo:make-cell:x"), 26 | 7)); 27 | $txt .= ShowLine(Op2("=", 28 | Op1("get!", Ref("demo:make-cell:x")), 29 | 7)); 30 | $txt .= ShowLine(Op("define", 31 | "demo:make-cell:y", 32 | Op("make-cell", 11))); 33 | $txt .= ShowLine(Op2("=", 34 | Op1("get!", Ref("demo:make-cell:y")), 35 | 11)); 36 | $txt .= ShowLine(Op2("set!", 37 | Ref("demo:make-cell:y"), 38 | 22)); 39 | $txt .= ShowLine(Op2("=", 40 | Op1("get!", Ref("demo:make-cell:y")), 41 | 22)); 42 | $txt .= ShowLine(Op2("=", 43 | Op1("get!", Ref("demo:make-cell:x")), 44 | 7)); 45 | $txt .= ShowLine(Op2("=", 46 | 29, 47 | TailOp2("+", 48 | Op1("get!", Ref("demo:make-cell:x")), 49 | Op1("get!", Ref("demo:make-cell:y"))))); 50 | $txt .= ShowLine(Op("if", 51 | Op("=", 52 | Op1("get!", 53 | Ref("demo:make-cell:x")), 54 | 7), 55 | Op2("set!", Ref("demo:make-cell:x"), 88), 56 | Op2("set!", Ref("demo:make-cell:x"), 99))); 57 | $txt .= ShowLine(Op2("=", 58 | Op1("get!", Ref("demo:make-cell:x")), 59 | 88)); 60 | $txt .= ShowLine(Op("if", 61 | Op("=", 62 | Op1("get!", 63 | Ref("demo:make-cell:x")), 64 | 7), 65 | Op2("set!", Ref("demo:make-cell:x"), 88), 66 | Op2("set!", Ref("demo:make-cell:x"), 99))); 67 | $txt .= ShowLine(Op2("=", 68 | Op1("get!", Ref("demo:make-cell:x")), 69 | 99)); 70 | 71 | return $txt; 72 | }; 73 | 74 | 75 | ShowLesson(ShowMutableLesson()); 76 | 77 | -------------------------------------------------------------------------------- /msg/COS_MutableStructure.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.header('OBJECT', 'introduce simple mutable structures'); 6 | 7 | cos.intro("mutable-struct"); 8 | cos.add(`define mutable-struct | ? x:list | 9 | let ((cell:list | map (? x | make-cell 0) $x:list)) | 10 | ? x:find | list-ref $cell:list | list:find $x:list $x:find`); 11 | 12 | cos.add(`define demo:mutable-struct | mutable-struct | vector x:1 x:2 x:3`); 13 | cos.add(`set! (demo:mutable-struct x:1) 15`); 14 | cos.add(`= 15 | get! | demo:mutable-struct x:1`); 15 | -------------------------------------------------------------------------------- /msg/COS_NOR.gate: -------------------------------------------------------------------------------- 1 | 8 5 1 0 2 | 1 6 0 1 IN1 3 | 3 6 0 1 4 | 5 6 0 1 5 | 7 6 0 1 6 | 8 7 1 0 7 | 1 8 0 1 IN2 8 | 3 8 0 1 9 | 5 8 0 1 10 | 7 8 0 1 11 | 8 9 1 0 12 | 9 10 0 1 13 | 11 10 0 1 14 | 13 10 0 1 15 | 15 10 0 1 16 | 17 10 0 1 17 | 19 10 0 1 18 | -------------------------------------------------------------------------------- /msg/COS_NOT.gate: -------------------------------------------------------------------------------- 1 | 8 5 1 0 2 | 1 6 0 1 IN 3 | 3 6 0 1 4 | 5 6 0 1 5 | 7 6 0 1 6 | 13 6 0 1 7 | 15 6 0 1 8 | 17 6 0 1 9 | 19 6 0 1 10 | 8 7 1 0 11 | 12 7 -1 0 12 | 9 8 0 1 13 | 11 8 0 1 14 | 15 | -------------------------------------------------------------------------------- /msg/COS_NotLogic.md: -------------------------------------------------------------------------------- 1 | # MATH introduce logical negation 2 | 3 | At this point, the listener can find numbers in our sentences, and has some 4 | idea of symbols related to equality and inequality. But the structure of the 5 | sentences remains a mystery. Let's introduce more math, so that we can show 6 | different sentence structures. First, let's introduce logical negation. 7 | We construct some sentences the listener should know are wrong, and put "not" 8 | in front of them. 9 | 10 | -------------------------------------------------------------------------------- /msg/COS_NotLogic.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowNotLogicLesson { 7 | my $txt = ""; 8 | 9 | $txt .= ShowLine(Op("intro","not")); 10 | 11 | $txt .= ShowDoc("Show an equality, then negate two conflicting inequalities."); 12 | 13 | my @example; 14 | my @example2; 15 | @example = prand(10,5); 16 | for (my $i=0; $i<=$#example; $i++) 17 | { 18 | my $r = $example[$i]; 19 | $txt .= ShowLine(Op2("=",ShowUnary($r),ShowUnary($r))); 20 | $txt .= ShowLine(TailOp1("not",Op2("<",ShowUnary($r),ShowUnary($r)))); 21 | $txt .= ShowLine(TailOp1("not",Op2(">",ShowUnary($r),ShowUnary($r)))); 22 | } 23 | $txt .= ShowDoc("Show an inequality, then two negations."); 24 | @example = prand(10,5); 25 | @example2 = prand(10,5); 26 | for (my $i=0; $i<=$#example; $i++) 27 | { 28 | my $r = $example[$i]; 29 | my $r2 = $r+1+$example2[$i]; 30 | $txt .= ShowLine(Op2("<",ShowUnary($r),ShowUnary($r2))); 31 | $txt .= ShowLine(TailOp1("not",Op2("=",ShowUnary($r),ShowUnary($r2)))); 32 | $txt .= ShowLine(TailOp1("not",Op2(">",ShowUnary($r),ShowUnary($r2)))); 33 | } 34 | $txt .= ShowDoc("Show another batch of inequalities with negations."); 35 | @example = prand(10,5); 36 | @example2 = prand(10,5); 37 | for (my $i=0; $i<=$#example; $i++) 38 | { 39 | my $r = $example[$i]; 40 | my $r2 = $r+1+$example2[$i]; 41 | $txt .= ShowLine(Op2(">",ShowUnary($r2),ShowUnary($r))); 42 | $txt .= ShowLine(TailOp1("not",Op2("=",ShowUnary($r2),ShowUnary($r)))); 43 | $txt .= ShowLine(TailOp1("not",Op2("<",ShowUnary($r2),ShowUnary($r)))); 44 | } 45 | 46 | return $txt; 47 | }; 48 | 49 | 50 | ShowLesson(ShowNotLogicLesson()); 51 | 52 | -------------------------------------------------------------------------------- /msg/COS_OR.gate: -------------------------------------------------------------------------------- 1 | 10 3 1 0 2 | 3 4 0 1 IN1 3 | 5 4 0 1 4 | 7 4 0 1 5 | 9 4 0 1 6 | 10 5 1 0 7 | 3 6 0 1 IN2 8 | 5 6 0 1 9 | 7 6 0 1 10 | 9 6 0 1 11 | 10 7 1 0 12 | 9 8 0 1 13 | 11 8 0 1 14 | 13 8 0 1 15 | 15 8 0 1 16 | 17 8 0 1 17 | -------------------------------------------------------------------------------- /msg/COS_OSC.gate: -------------------------------------------------------------------------------- 1 | 9 6 0 -1 2 | 11 6 0 -1 3 | 8 7 1 0 4 | 12 7 -1 0 5 | 5 8 0 1 6 | 7 8 0 1 7 | 9 8 0 1 8 | 11 8 0 1 9 | 13 8 0 1 10 | 15 8 0 1 11 | -------------------------------------------------------------------------------- /msg/COS_OrLogic.md: -------------------------------------------------------------------------------- 1 | # MATH introduce the OR logical operator 2 | 3 | Here is `or`, which evaluates to `true` if either of its arguments are `true`, 4 | and `false` otherwise. Again, we just don't talk about what happens if you 5 | pass in any unexpected values, like integers or functions. The message is 6 | constructed so that the problem never comes up. 7 | -------------------------------------------------------------------------------- /msg/COS_OrLogic.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowOrLogicLesson { 7 | my $txt = ""; 8 | 9 | $txt .= ShowLine(Op("intro","or")); 10 | $txt .= 'define or | ? x | ? y | if $x $true $y' . "\n"; 11 | 12 | $txt .= 'not | or $false $false;' . "\n"; 13 | $txt .= 'or $false $true;' . "\n"; 14 | $txt .= 'or $true $false;' . "\n"; 15 | $txt .= 'or $true $true;' . "\n"; 16 | $txt .= '= $false | or $false $false;' . "\n"; 17 | $txt .= '= $true | or $false $true;' . "\n"; 18 | $txt .= '= $true | or $true $false;' . "\n"; 19 | $txt .= '= $true | or $true $true;' . "\n"; 20 | 21 | for (my $i=0; $i<10; $i++) 22 | { 23 | $txt .= ShowLine(Op2("or",ShowTrueComparison(),ShowTrueComparison())); 24 | } 25 | 26 | for (my $i=0; $i<5; $i++) 27 | { 28 | $txt .= ShowLine(Op2("or",ShowTrueComparison(),ShowFalseComparison())); 29 | } 30 | 31 | for (my $i=0; $i<5; $i++) 32 | { 33 | $txt .= ShowLine(Op2("or",ShowFalseComparison(),ShowTrueComparison())); 34 | } 35 | 36 | for (my $i=0; $i<5; $i++) 37 | { 38 | $txt .= ShowLine(Op1("not", 39 | Op2("or", 40 | ShowFalseComparison(), 41 | ShowFalseComparison()))); 42 | } 43 | 44 | for (my $i=0; $i<10; $i++) 45 | { 46 | my $t1 = irand(2); 47 | my $t2 = irand(2); 48 | my $c1 = ""; 49 | my $c2 = ""; 50 | if ($t1==1) 51 | { 52 | $c1 = ShowTrueComparison(); 53 | } 54 | else 55 | { 56 | $c1 = ShowFalseComparison(); 57 | } 58 | if ($t2==1) 59 | { 60 | $c2 = ShowTrueComparison(); 61 | } 62 | else 63 | { 64 | $c2 = ShowFalseComparison(); 65 | } 66 | my $c = Op2("or",$c1,$c2); 67 | 68 | if (!(($t1==1)||($t2==1))) 69 | { 70 | $c = Op1("not",$c); 71 | } 72 | $txt .= ShowLine($c); 73 | } 74 | 75 | $txt .= "~ Now is an opportune moment to add `<=` and `>=` definitions.\n"; 76 | $txt .= "~ There are shorter definitions, like just negating `>` and `<`, but this feels more natural?\n"; 77 | $txt .= 'define >= | ? x | ? y | or (> $x $y) (= $x $y);' . "\n"; 78 | $txt .= 'define <= | ? x | ? y | or (< $x $y) (= $x $y);' . "\n"; 79 | for (my $i=0; $i<3; $i++) { 80 | for (my $j=0; $j<3; $j++) { 81 | $txt .= "(" . (($i>=$j)?"":"not / ") . ">= $i $j);\n"; 82 | $txt .= "(" . (($i<=$j)?"":"not / ") . "<= $i $j);\n"; 83 | } 84 | } 85 | 86 | 87 | return $txt; 88 | } 89 | 90 | 91 | ShowLesson(ShowOrLogicLesson()); 92 | 93 | -------------------------------------------------------------------------------- /msg/COS_Quantifier.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowQuantifierLesson { 7 | my $txt = ""; 8 | $txt .= "# MATH introduce universal quantifier\n"; 9 | $txt .= "# really need to link with sets for true correctness\n"; 10 | $txt .= "# and the examples here are REALLY sparse, need much more\n"; 11 | $txt .= ShowLine(Op("intro","forall")); 12 | for (my $i=5; $i>=0; $i--) 13 | { 14 | $txt .= ShowLine(Op2("<", 15 | Num($i), 16 | Op2("+",Num($i),Num(1)))); 17 | } 18 | $txt .= ShowLine(Op("forall", 19 | Proc(Lit("x"), 20 | Op2("<", 21 | Ref("x"), 22 | Op2("+",Ref("x"),Num(1)))))); 23 | for (my $i=5; $i>=0; $i--) 24 | { 25 | my $txt0 = Op2("<", 26 | Num($i), 27 | Op2("*",Num($i),Num(2))); 28 | if (!($i<$i*2)) 29 | { 30 | $txt0 = Op1("not",$txt0); 31 | } 32 | $txt .= ShowLine($txt0); 33 | } 34 | $txt .= ShowLine(Op1("not", 35 | Op("forall", 36 | Proc(Lit("x"), 37 | Op2("<", 38 | Ref("x"), 39 | Op2("*",Ref("x"),Num(2))))))); 40 | $txt .= "# MATH introduce existential quantifier\n"; 41 | $txt .= "# really need to link with sets for true correctness\n"; 42 | $txt .= "# and the examples here are REALLY sparse, need much more\n"; 43 | for (my $i=5; $i>=0; $i--) 44 | { 45 | my $txt0 = Op2("=", 46 | Num($i), 47 | Op2("*",Num(2),Num(2))); 48 | if (!($i==2*2)) 49 | { 50 | $txt0 = Op1("not",$txt0); 51 | } 52 | $txt .= ShowLine($txt0); 53 | } 54 | $txt .= ShowLine(Op("intro","exists")); 55 | $txt .= ShowLine(Op("exists", 56 | Proc(Lit("x"), 57 | Op2("=", 58 | Ref("x"), 59 | Op2("*",Num(2),Num(2)))))); 60 | 61 | for (my $i=5; $i>=0; $i--) 62 | { 63 | my $txt0 = Op2("=", 64 | Num($i), 65 | Op2("+",Num($i),Num(2))); 66 | if (!($i==$i+1)) 67 | { 68 | $txt0 = Op1("not",$txt0); 69 | } 70 | $txt .= ShowLine($txt0); 71 | } 72 | $txt .= ShowLine(Op("not", 73 | Op("exists", 74 | Proc(Lit("x"), 75 | Op2("=", 76 | Ref("x"), 77 | Op2("+",Ref("x"),Num(2))))))); 78 | 79 | return $txt; 80 | }; 81 | 82 | 83 | ShowLesson(ShowQuantifierLesson()); 84 | 85 | -------------------------------------------------------------------------------- /msg/COS_Quote.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.header('MATH', 'quoting syntax'); 6 | 7 | cos.add(`define quote-f | ? key | ? x | 8 | if (not | function? $x) $x | 9 | if (= $key | list-ref $x 0) (tail $x) | 10 | prepend vector | map (quote-f $key) $x`); 11 | 12 | cos.add(`= (quote-f x 1) 1`); 13 | cos.add(`list= (quote-f x | vector 1) | vector vector 1`); 14 | cos.add(`list= (quote-f x | vector 1 2 3) | vector vector 1 2 3`); 15 | cos.add(`list= (quote-f x | vector 1 (vector 5 2) 3) | vector vector 1 (vector vector 5 2) 3`); 16 | cos.add(`list= (quote-f x | vector 1 (vector 5 2) (vector x + 5 2)) | vector vector 1 (vector vector 5 2) (vector + 5 2)`); 17 | 18 | cos.intro("quote"); 19 | cos.add(`define translate | assign prev $translate | ? x | 20 | if (not | function? $x) $x | 21 | if (not | = quote | head $x) (prev $x) | 22 | translate | quote-f (list-ref $x 1) (list-ref $x 2)`); 23 | 24 | cos.add(`= (quote x 1) 1`); 25 | cos.add(`list= (quote x | 1) | vector 1`); 26 | cos.add(`list= (quote x | 1 2 3) | vector 1 2 3`); 27 | cos.add(`list= (quote x | 1 (5 2) 3) | vector 1 (vector 5 2) 3`); 28 | cos.add(`list= (quote x | 1 (5 2) (x + 5 2)) | vector 1 (vector 5 2) 7`); 29 | -------------------------------------------------------------------------------- /msg/COS_SR.gate: -------------------------------------------------------------------------------- 1 | 1 2 0 1 SET 2 | 3 2 0 1 3 | 5 2 0 1 4 | 7 2 0 1 5 | 9 2 0 1 6 | 11 2 0 1 7 | 13 2 0 1 8 | 14 3 1 0 9 | 9 4 0 -1 10 | 11 4 0 -1 11 | 13 4 0 -1 12 | 15 4 0 -1 13 | 8 5 1 0 14 | 12 5 -1 0 15 | 5 6 0 1 16 | 7 6 0 1 17 | 9 6 0 1 18 | 11 6 0 1 19 | 6 7 -1 0 20 | 10 7 1 0 21 | 1 8 0 1 RESET 22 | 3 8 0 1 23 | 5 8 0 1 24 | 11 8 0 1 25 | 13 8 0 1 26 | 15 8 0 1 27 | 17 8 0 1 28 | 19 8 0 1 29 | 30 | -------------------------------------------------------------------------------- /msg/COS_Series.md: -------------------------------------------------------------------------------- 1 | # MATH introduce pi and e 2 | 3 | Just in passing, give approximate values for `pi` and `e`. 4 | -------------------------------------------------------------------------------- /msg/COS_Set.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.seed(42); 6 | cos.add('intro element'); 7 | cos.add('define element | ? x | ? y:list | not | = $undefined | list:find $y:list $x'); 8 | 9 | for (let i=0; i<5; i++) { 10 | const hset = new Map(); 11 | const lst = []; 12 | for (let j=0; j<6; j++) { 13 | const x = cos.irand(10); 14 | if (!hset.has(x)) { 15 | hset.set(x, 1); 16 | lst.push(x); 17 | } 18 | } 19 | for (let j=0; j<3; j++) { 20 | const mem = lst[cos.irand(lst.length)]; 21 | cos.add(`element ${mem} | ${cos.vector(lst)}`); 22 | } 23 | } 24 | 25 | for (let i=0; i<5; i++) { 26 | const hset = new Map(); 27 | const lst = []; 28 | for (let j=0; j<6; j++) { 29 | const x = cos.irand(10); 30 | if (!hset.has(x)) { 31 | hset.set(x, 1); 32 | lst.push(x); 33 | } 34 | } 35 | const mem = lst.shift(); 36 | cos.add(`not | element ${mem} | ${cos.vector(lst)}`); 37 | } 38 | 39 | cos.doc('Set some rules for set equality.'); 40 | 41 | cos.intro("set:<="); 42 | cos.add(`define set:<= | ? x | ? y | 43 | if (= 0 | list-length $x) $true | 44 | and (element (head $x) $y) | 45 | set:<= (tail $x) $y`); 46 | 47 | cos.intro("set:="); 48 | cos.add(`define set:= | ? x | ? y | 49 | and (set:<= $x $y) (set:<= $y $x)`); 50 | 51 | cos.add(`set:= (vector 1 5 9) (vector 5 1 9)`); 52 | cos.add(`set:= (vector 1 5 9) (vector 9 1 5)`); 53 | cos.add(`not | set:= (vector 1 5 9) (vector 1 5)`) 54 | 55 | cos.doc(`let's go leave ourselves wide open to Russell's paradox 56 | by using characteristic functions since it doesn't really matter 57 | within the bounds of this message`); 58 | 59 | cos.add(`element 5 | all | ? x | = 15 | + $x 10`); 60 | cos.add(`element 3 | all | ? x | = (* $x 3) (+ $x 6)`); 61 | 62 | cos.intro("set:0"); 63 | cos.add(`define set:0 | vector`); 64 | cos.add(`element 0 $set:int:+`); 65 | cos.add(`forall | ? x | => (element $x $set:int:+) (element (+ $x 1) $set:int:+)`); 66 | 67 | for (let i=1; i<10; i++) { 68 | cos.add(`element ${i} $set:int:+`); 69 | } 70 | 71 | cos.add(`define set:true:false | vector $true $false`); 72 | cos.add(`element $true $set:true:false`); 73 | cos.add(`element $false $set:true:false`); 74 | 75 | cos.add(`define set:even | all | ? x | exists | ? y | 76 | and (element $y $set:int:+) (= $x | * 2 $y)`); 77 | 78 | for (let i=0; i<=6; i++) { 79 | cos.add(`element ${i} $set:int:+`) 80 | cos.add(`${(i % 2 === 0) ? '' : 'not | '}element ${i} $set:even`); 81 | } 82 | -------------------------------------------------------------------------------- /msg/COS_Set.md: -------------------------------------------------------------------------------- 1 | # MATH introduce sets and set membership 2 | -------------------------------------------------------------------------------- /msg/COS_Solve.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var cos = require("./cosmic"); 4 | 5 | cos.doc("# MATH introduce solving"); 6 | 7 | cos.add("intro solve"); 8 | 9 | cos.add("= 5 | solve | ? x | = $x 5"); 10 | cos.add("= 9 | solve | ? x | = 10 | + $x 1"); 11 | cos.add("= 2 | solve | ? x | and (= 4 | * $x $x) (> $x 0)"); 12 | cos.add("= (minus 2) | solve | ? x | and (= 4 | * $x $x) (< $x 0)"); 13 | 14 | cos.add("intro sqrt"); 15 | cos.add("= 2 | * (sqrt 2) (sqrt 2)"); 16 | cos.add("= (sqrt 2) | solve | ? x | and (> $x 0) (= 2 | * $x $x)"); 17 | 18 | cos.add("= 2 | + (frac 3 2) (frac 1 2)"); 19 | 20 | cos.add("= (minus 1) | * $i $i"); 21 | -------------------------------------------------------------------------------- /msg/COS_Subtraction.md: -------------------------------------------------------------------------------- 1 | # MATH introduce subtraction 2 | 3 | Introduce subtraction via `= X | - Y Z` sentences. Until syntax is fully understood, 4 | an ambiguity may remain between this and addition. 5 | -------------------------------------------------------------------------------- /msg/COS_Subtraction.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowSubtractionLesson { 7 | my $txt = ""; 8 | $txt .= ShowLine(Op("intro","-")); 9 | for (my $i=0; $i<10; $i++) 10 | { 11 | my $r = irand(5); 12 | my $r2 = irand(5); 13 | $txt .= ShowLine(TailOp2("=", 14 | ShowUnary($r), 15 | Op2("-", 16 | ShowUnary($r+$r2), 17 | ShowUnary($r2)))); 18 | } 19 | return $txt; 20 | }; 21 | 22 | 23 | ShowLesson(ShowSubtractionLesson()); 24 | 25 | -------------------------------------------------------------------------------- /msg/COS_Translate.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.comment("SYNTAX how to change the imagined interpreter"); 6 | cos.add("intro translate"); 7 | cos.add("define translate:begin $translate"); 8 | cos.add("define translate | ? x | if (= $x 32) 64 | translate:begin $x"); 9 | cos.add("= 32 64"); 10 | cos.add("= (+ 32 64) 128"); 11 | cos.add("define translate $translate:begin"); 12 | cos.add("not | = 32 64"); 13 | cos.add("= (+ 32 64) 96"); 14 | 15 | cos.doc("Now let's do something useful: define a special form for lists."); 16 | cos.intro("vector"); 17 | cos.add(` 18 | define translate | ? x | 19 | if (not | function? $x) (translate:begin $x) | 20 | if (not | = vector | head $x) (translate:begin $x) | 21 | translate | prepend ((list 2) list | list-length | tail $x) | tail $x`); 22 | cos.add("list= (vector 1 2 3) | (list 3) 1 2 3"); 23 | -------------------------------------------------------------------------------- /msg/COS_TrueFalse.md: -------------------------------------------------------------------------------- 1 | # MATH introduce true and false 2 | 3 | Now that we have functions, we could introduce some clever definitions of true, false, 4 | and conditionals, where: 5 | 6 | * `if` is `? x | ? y | ? z | x $y $z;` 7 | * `true` is `? y | ? z | y;` 8 | * `false` is `? y | ? z | z;` 9 | 10 | This is a neat implementation, but maybe a bit confusing. So let's 11 | not actually commit to a type for truth values in the message yet, 12 | but just equate them with the results of equality `=`. 13 | 14 | Once we have truth values, we can introduce conditionals and build up to fun stuff. 15 | 16 | One slightly sneaky thing we do is to code `true` and `false` as `$1` 17 | and `$0`. This could be helpful, or confusing, I'm not sure. Nothing 18 | else in the message depends on this so it can be adjusted to taste. 19 | -------------------------------------------------------------------------------- /msg/COS_TrueFalse.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowTrueFalseLesson { 7 | my $txt = ""; 8 | $txt .= ShowLine(Op("intro", "true")); 9 | $txt .= ShowLine(Op("intro", "false")); 10 | $txt .= ShowLine(Op("define","true",Op2("=",0,0))); 11 | $txt .= ShowLine(Op("define","false",Op2("=",0,1))); 12 | for (my $i=0; $i<5; $i++) 13 | { 14 | $txt .= ShowLine(Op2("=",ShowTrue(),ShowTrueComparisonBin())); 15 | } 16 | for (my $i=0; $i<5; $i++) 17 | { 18 | $txt .= ShowLine(Op2("=",ShowTrueComparisonBin(),ShowTrue())); 19 | } 20 | for (my $i=0; $i<5; $i++) 21 | { 22 | $txt .= ShowLine(Op2("=",ShowFalse(),ShowFalseComparisonBin())); 23 | } 24 | for (my $i=0; $i<5; $i++) 25 | { 26 | $txt .= ShowLine(Op2("=",ShowFalseComparisonBin(),ShowFalse())); 27 | } 28 | $txt .= ShowLine(Op2("=",ShowTrue(),ShowTrue())); 29 | $txt .= ShowLine(Op2("=",ShowFalse(),ShowFalse())); 30 | $txt .= ShowLine(Op1("not",Op2("=",ShowTrue(),ShowFalse()))); 31 | $txt .= ShowLine(Op1("not",Op2("=",ShowFalse(),ShowTrue()))); 32 | 33 | for (my $i=0; $i<5; $i++) 34 | { 35 | $txt .= ShowLine(Op2("=",ShowTrueComparisonBin(),ShowTrueComparisonBin())); 36 | } 37 | for (my $i=0; $i<5; $i++) 38 | { 39 | $txt .= ShowLine(Op2("=",ShowFalseComparisonBin(),ShowFalseComparisonBin())); 40 | } 41 | for (my $i=0; $i<5; $i++) 42 | { 43 | $txt .= ShowLine(Op1("not", 44 | Op2("=", 45 | ShowFalseComparisonBin(), 46 | ShowTrueComparisonBin()))); 47 | } 48 | for (my $i=0; $i<5; $i++) 49 | { 50 | $txt .= ShowLine(Op1("not", 51 | Op2("=", 52 | ShowTrueComparisonBin(), 53 | ShowFalseComparisonBin()))); 54 | } 55 | 56 | return $txt; 57 | }; 58 | 59 | 60 | ShowLesson(ShowTrueFalseLesson()); 61 | 62 | -------------------------------------------------------------------------------- /msg/COS_Turing.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const cos = require('./cosmic'); 4 | 5 | cos.add(`define tape:tail | ? x | 6 | if (>= 1 | list-length $x) (vector | vector) | 7 | tail $x`); 8 | 9 | cos.add(`define tape:head | ? x | 10 | if (= 0 | list-length $x) (vector) | 11 | head $x`); 12 | 13 | cos.add(`define tape:get | ? tape | tape:head | second $tape`); 14 | 15 | cos.add(`define tape:next | lambda (tape n x) | 16 | if (= $n 1) (pair (prepend $x | first $tape) (tape:tail | second $tape)) | 17 | if (= $n 0) (pair (tape:tail | first $tape) (prepend (tape:head | first $tape) (prepend $x (tape:tail | second $tape)))) | 18 | pair (first $tape) (prepend $x (tape:tail | second $tape))`); 19 | 20 | cos.intro("tape:do"); 21 | cos.add(`define tape:do | lambda (x:function current end tape) | 22 | if (= $current $end) $tape | 23 | let ((next | x:function $current | tape:get $tape)) | 24 | tape:do $x:function (list-ref $next 0) $end | 25 | tape:next $tape (list-ref $next 1) (list-ref $next 2)`); 26 | 27 | cos.intro("tape:make"); 28 | cos.add(`define tape:make | ? x | pair (vector) $x`); 29 | 30 | cos.add(`define tape:-:tail | ? x | ? x:list | 31 | if (= 0 | list-length $x:list) $x:list | 32 | if (not | equal $x | last $x:list) $x:list | 33 | tape:-:tail $x (except-last $x:list)`); 34 | 35 | cos.intro("tape:result"); 36 | cos.add(`define tape:result | ? x | tape:-:tail (vector) (second $x)`); 37 | 38 | cos.intro("demo:tape:function:+:1"); 39 | cos.add(`define demo:tape:function:+:1 | make-hash | vector 40 | (pair next (make-hash | vector 41 | (pair 0 (vector next 1 0)) 42 | (pair 1 (vector next 1 1)) 43 | (pair (vector) (vector +:1 0 (vector))))) 44 | (pair +:1 (make-hash | vector 45 | (pair 0 (vector not:+:1 0 1)) 46 | (pair 1 (vector +:1 0 0)) 47 | (pair (vector) (vector end 2 1)))) 48 | (pair not:+:1 (make-hash | vector 49 | (pair 0 (vector not:+:1 0 0)) 50 | (pair 1 (vector not:+:1 0 1)) 51 | (pair (vector) (vector end 1 (vector))))) 52 | (pair end (make-hash | vector))`); 53 | 54 | cos.add(`list= (vector 1 0 1 0) | tape:result | 55 | tape:do $demo:tape:function:+:1 next end (tape:make | vector 1 0 0 1)`); 56 | 57 | cos.add(`list= (vector 1 0 0 0) | tape:result | 58 | tape:do $demo:tape:function:+:1 next end (tape:make | vector 1 1 1)`); 59 | 60 | cos.add(`list= (vector 1 1 1 0 0 1 0 0 0) | tape:result | 61 | tape:do $demo:tape:function:+:1 next end (tape:make | vector 1 1 1 0 0 0 1 1 1)`); 62 | -------------------------------------------------------------------------------- /msg/COS_Turing.md: -------------------------------------------------------------------------------- 1 | # TURING introduce turing machine model 2 | 3 | This is just for fun, as an exercise. 4 | -------------------------------------------------------------------------------- /msg/COS_TypedGraph.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use strict; 3 | 4 | use cosmic; 5 | 6 | sub ShowTypedGraphLesson { 7 | my $txt = ""; 8 | $txt .= "# MATH introduce graph structures\n"; 9 | 10 | $txt .= "# really need some type templating - will define this later\n"; 11 | $txt .= "# type system needs a little bit of dis-ambiguating!\n"; 12 | $txt .= ShowLine(Op1("pending", "template")); 13 | $txt .= ShowLine(Op2("define", 14 | "tuple", 15 | Template(["T"], 16 | ProcTyped(["lst", Op("listof", Ref("T"))], 17 | Op("type", 18 | Op("tupleof", 19 | Ref("T"), 20 | Op1("list-length", Ref("lst"))), 21 | Ref("lst")))))); 22 | 23 | $txt .= ShowLine(Op2("define", 24 | "tuple-ref", 25 | Template(["T", "len"], 26 | ProcTyped(["x", Op("tupleof", Ref("T"), Ref("len")), 27 | "y", "integer"], 28 | Op("list-ref", 29 | Op("get-raw", Ref("x")), 30 | Op("get-raw", Ref("y"))))))); 31 | 32 | $txt .= ShowLine("define", 33 | "make-graph", 34 | ProcTyped(["nodes", Op("listof", "integer"), 35 | "links", Op("listof", Op("tupleof", "integer", 2))], 36 | Op2("type", 37 | "graph", 38 | Op1("tuple", 39 | Op("list", 40 | Ref("nodes"), 41 | Ref("links")))))); 42 | 43 | $txt .= ShowLine(Op2("define", 44 | "test-graph", 45 | Op("make-graph", 46 | ShowList("g1", "g2", "g3", "g4"), 47 | Op("list", 48 | Op("tuple", ShowList("g1", "g2")), 49 | Op("tuple", ShowList("g2", "g3")), 50 | Op("tuple", ShowList("g1", "g4")))))); 51 | 52 | $txt .= ShowLine(Op2("define", 53 | "graph-linked", 54 | ProcTyped(["g", "graph", 55 | "n1", "integer", 56 | "n2", "integer"], 57 | Op1("exists", 58 | Proc("idx", 59 | Op2("=", 60 | Op2("tuple-ref", 61 | Op2("tuple-ref", 62 | Ref("g"), 63 | 1), 64 | Ref("idx")), 65 | Op1("tuple", 66 | Op("list", 67 | Ref("n1"), 68 | Ref("n2"))))))))); 69 | $txt .= ShowLine(Op2("=", 70 | Op("graph-linked", Ref("test-graph"), "g1", "g2"), 71 | Op("true"))); 72 | $txt .= ShowLine(Op2("=", 73 | Op("graph-linked", Ref("test-graph"), "g1", "g3"), 74 | Op("false"))); 75 | $txt .= ShowLine(Op2("=", 76 | Op("graph-linked", Ref("test-graph"), "g2", "g4"), 77 | Op("false"))); 78 | $txt .= ShowLine(Op2("define", 79 | "graph-linked*", 80 | ProcMultiple(["g", "n1", "n2"], 81 | Op2("or", 82 | Op2("=", Ref("n1"), Ref("n2")), 83 | Op2("or", 84 | Op("graph-linked", 85 | Ref("g"), 86 | Ref("n1"), 87 | Ref("n2")), 88 | Op("exists", 89 | Proc("n3", 90 | Op2("and", 91 | Op("graph-linked", 92 | Ref("g"), 93 | Ref("n1"), 94 | Ref("n3")), 95 | Op("graph-linked*", 96 | Ref("g"), 97 | Ref("n3"), 98 | Ref("n2")))))))))); 99 | 100 | $txt .= ShowLine(Op2("=", 101 | Op("graph-linked*", Ref("test-graph"), "g1", "g2"), 102 | Op("true"))); 103 | $txt .= ShowLine(Op2("=", 104 | Op("graph-linked*", Ref("test-graph"), "g1", "g3"), 105 | Op("true"))); 106 | $txt .= ShowLine(Op2("=", 107 | Op("graph-linked*", Ref("test-graph"), "g2", "g4"), 108 | Op("false"))); 109 | 110 | return $txt; 111 | }; 112 | 113 | 114 | ShowLesson(ShowTypedGraphLesson()); 115 | 116 | -------------------------------------------------------------------------------- /msg/COS_UnlessProbe.scm: -------------------------------------------------------------------------------- 1 | 2 | # GATE probing networks of unless gates 3 | 4 | (define set-input | ? circuit | ? index | ? value | 5 | assign wire (list-ref $circuit $index) | 6 | map (? w | if (not | equal $w $wire) $w | 7 | vector (list-ref $w 0) 8 | (list-ref $w 1) 9 | (list-ref $w 2) 10 | (list-ref $w 3) 11 | $value) 12 | $circuit); 13 | 14 | (define read-output | ? circuit | ? index | 15 | assign len (list-length $circuit) | 16 | assign wire (list-ref $circuit | - (- $len 1) $index) | 17 | list-ref $wire 4); 18 | 19 | (define sim | ? circuit | ? steps | ? setter | 20 | if (= $steps 0) $circuit | 21 | sim (simulate-unless | setter $circuit) (- $steps 1) $setter); 22 | 23 | (define smart-sim | ? circuit | ? setter | 24 | sim $circuit (list-length $circuit) $setter); 25 | 26 | 27 | # test cos_not gate 28 | 29 | (define cos_not_harness | ? x | 30 | assign c $cos_not_gate | 31 | assign c (smart-sim $c | ? c | set-input $c 0 $x) | 32 | read-output $c 0); 33 | 34 | (= $false | cos_not_harness $true); 35 | 36 | (= $true | cos_not_harness $false); 37 | 38 | # test cos_and gate 39 | 40 | (define cos_and_harness | ? x | ? y | 41 | assign c $cos_and_gate | 42 | assign c (smart-sim $c | ? c | set-input (set-input $c 0 $x) 1 $y) | 43 | read-output $c 0); 44 | 45 | (= $false | cos_and_harness $false $false); 46 | (= $false | cos_and_harness $false $true); 47 | (= $false | cos_and_harness $true $false); 48 | (= $true | cos_and_harness $true $true); 49 | 50 | # this code is more awkward than it needs to be - 51 | # should make circuits mutable 52 | -------------------------------------------------------------------------------- /msg/DEPEND.cmake: -------------------------------------------------------------------------------- 1 | 2 | # set(req_COS_Intro COS_License) 3 | set(req_COS_AndLogic COS_TrueFalse) 4 | set(req_COS_OrLogic COS_TrueFalse) 5 | set(req_COS_ListPre COS_Cons COS_TrueFalse) 6 | set(req_COS_Map COS_ListPre) 7 | set(req_COS_Series COS_Map COS_OrLogic COS_If COS_List) 8 | set(req_COS_Translate COS_ListPre) 9 | set(req_COS_List COS_ListPre COS_Translate COS_OrLogic) 10 | set(req_COS_Complex COS_List COS_Map COS_Series) 11 | set(req_COS_Dot COS_List) 12 | set(req_COS_Element COS_List COS_Series COS_Hash COS_NewType) 13 | set(req_COS_Let COS_List) 14 | set(req_COS_MultipleParameter COS_Let) 15 | set(req_COS_Implication COS_TrueFalse) 16 | set(req_COS_Set COS_List COS_Implication) 17 | set(req_COS_Graph COS_MultipleParameter) 18 | set(req_COS_Begin COS_Let COS_Mutable) 19 | set(req_COS_Hash COS_MultipleParameter) 20 | set(req_COS_MutableStructure COS_List COS_Let COS_Map) 21 | set(req_COS_Turing COS_MultipleParameter COS_Hash) 22 | set(req_COS_Quote COS_List COS_Map) 23 | set(req_COS_NewType COS_MultipleParameter COS_Set COS_Quote COS_Begin) 24 | set(req_COS_Unless COS_TrueFalse COS_Hash COS_Begin COS_Map COS_Series) 25 | set(req_COS_NOT COS_Unless) 26 | set(req_COS_AND COS_Unless) 27 | set(req_COS_OR COS_Unless) 28 | set(req_COS_NOR COS_Unless) 29 | set(req_COS_OSC COS_Unless) 30 | set(req_COS_SR COS_Unless) 31 | set(req_COS_D COS_Unless) 32 | set(req_COS_UnlessProbe COS_NOT COS_AND) 33 | set(req_COS_Message COS_Intro COS_List COS_Quote) 34 | set(req_COS_JavaPrep COS_NewType COS_Series COS_Hash) 35 | set(req_COS_JavaTest COS_JavaPrep) 36 | set(req_COS_JavaValidate COS_JavaTest) 37 | set(req_COS_Geo COS_NewType COS_Hash) 38 | set(req_COS_JList_impl COS_JavaPrep) 39 | set(req_COS_JList_test COS_JList_impl) 40 | set(req_COS_JListIterator_impl COS_JList_impl) 41 | set(req_COS_JDoor COS_JavaPrep) 42 | set(req_COS_JThing COS_JavaPrep) 43 | set(req_COS_JRoom COS_JavaPrep) 44 | set(req_COS_JNamed COS_JavaPrep) 45 | set(req_COS_JWorld COS_JavaPrep) 46 | set(req_COS_JRobo COS_JavaPrep) 47 | set(req_COS_JRoom_test COS_JRoom COS_JWorld COS_JDoor COS_JRobo COS_JThing COS_JNamed 48 | COS_JList_impl COS_JListIterator_impl) 49 | -------------------------------------------------------------------------------- /msg/PARTIAL.cmake: -------------------------------------------------------------------------------- 1 | # This is a subsection of the message with a more curated vocabulary 2 | # used for making http://cosmicos.github.io/next.html 3 | # (the larger message currently has all sorts of random stuff) 4 | 5 | set(COSMIC_DEPENDS 6 | COS_License 7 | COS_Intro 8 | COS_Compare 9 | COS_NotLogic 10 | COS_Unary 11 | COS_Addition 12 | COS_Subtraction 13 | COS_Multiplication 14 | COS_Binary 15 | COS_Evaluation 16 | COS_DefineFunction 17 | COS_TrueFalse 18 | COS_If 19 | COS_AndLogic 20 | COS_OrLogic 21 | COS_Cons 22 | COS_Mutable) 23 | -------------------------------------------------------------------------------- /msg/README.cmake: -------------------------------------------------------------------------------- 1 | # In this file, we list the active chapters to be included in the default message, 2 | # in order. 3 | 4 | set(COSMIC_DEPENDS 5 | COS_License 6 | COS_Intro 7 | COS_Compare 8 | COS_NotLogic 9 | COS_Addition 10 | COS_Subtraction 11 | COS_Multiplication 12 | COS_Binary 13 | COS_Evaluation 14 | COS_DefineFunction 15 | COS_TrueFalse 16 | COS_If 17 | COS_AndLogic 18 | COS_OrLogic 19 | COS_Cons 20 | COS_Mutable 21 | COS_ListPre 22 | COS_Map 23 | COS_Translate 24 | COS_List 25 | COS_Series 26 | COS_Complex 27 | COS_Let 28 | COS_MultipleParameter 29 | COS_CommentBase 30 | COS_Pure 31 | COS_Quantifier 32 | COS_Implication 33 | COS_Set 34 | COS_Graph 35 | COS_Begin 36 | COS_Hash 37 | COS_MutableStructure 38 | COS_Turing 39 | COS_Quote 40 | COS_NewType 41 | COS_Element 42 | COS_CommentBase2 43 | COS_Unless 44 | COS_NOT 45 | COS_AND 46 | COS_OR 47 | COS_NOR 48 | COS_OSC 49 | COS_SR 50 | COS_D 51 | COS_UnlessProbe 52 | COS_CommentBase3 53 | COS_Message 54 | COS_MARK 55 | COS_JavaPrep 56 | COS_JavaTest 57 | COS_JavaValidate 58 | COS_Geo 59 | COS_JList_impl 60 | COS_JList 61 | COS_JHashMap 62 | COS_JList_test 63 | COS_JListIterator_impl 64 | COS_JListIterator 65 | COS_JDoor 66 | COS_JThing 67 | COS_JRoom 68 | COS_JNamed 69 | COS_JWorld 70 | COS_JRobo 71 | COS_JRoom_test) 72 | 73 | # touch 1 74 | -------------------------------------------------------------------------------- /msg/names.json: -------------------------------------------------------------------------------- 1 | [ 2 | ["define", "@"], 3 | ["make-cell", "cell:make"], 4 | ["max", ">:>"], 5 | ["min", "<:<"], 6 | ["factorial", "*:<"], 7 | ["and", "true:*"], 8 | ["or", "true:+"], 9 | [">=", ">:="], 10 | ["<=", "<:="], 11 | ["car", "cons:0"], 12 | ["cdr", "cons:1"], 13 | ["set!", "cell:assign"], 14 | ["get!", "cell:get"], 15 | ["pair", "list:2"], 16 | ["head", "list:0"], 17 | ["tail", "list:remainder"] 18 | ] 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cosmicos", 3 | "version": "1.0.2", 4 | "description": "CosmicOS", 5 | "url": "https://github.com/paulfitz/cosmicos", 6 | "keywords": [ 7 | "space", 8 | "the", 9 | "final", 10 | "frontier" 11 | ], 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/paulfitz/cosmicos.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/paulfitz/cosmicos/issues" 18 | }, 19 | "author": { 20 | "name": "Paul Fitzpatrick", 21 | "email": "paulfitz@alum.mit.edu", 22 | "url": "https://paulfitz.github.io/" 23 | }, 24 | "dependencies": { 25 | "canvas": "^2.6.0", 26 | "fs-extra": "^8.1.0" 27 | }, 28 | "devDependencies": { 29 | "@types/fs-extra": "^8.0.0", 30 | "@types/mocha": "^5.2.7", 31 | "@types/node": "10.12.2", 32 | "mocha": "^6.2.2", 33 | "ts-node": "^8.3.0", 34 | "typescript": "3.4.5", 35 | "webpack": "^4.41.2", 36 | "webpack-cli": "^3.3.10" 37 | }, 38 | "scripts": { 39 | "old-prepublish": "mkdir -p build && mkdir -p lib && mkdir -p bin && cd build && cmake .. && make cli && cp lib/cosmicos.js ../lib && cp bin/cosmicos.js ../bin", 40 | "old-test": "cd build && make" 41 | }, 42 | "main": "lib/cosmicos.js", 43 | "license": "GPL", 44 | "bin": "bin/cosmicos.js" 45 | } 46 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | 2 | COSMIC_SITE=$(PWD)/../site/cosmicos 3 | 4 | HAXELANG=js 5 | #HAXELANG=php 6 | #HAXELANG=cpp 7 | 8 | default: audio eval scrawl 9 | 10 | audio: 11 | haxe -$(HAXELANG) CosmicAudio.$(HAXELANG) -main cosmicos.Sound 12 | 13 | eval: 14 | haxe -$(HAXELANG) CosmicEval.$(HAXELANG) -main cosmicos.Evaluate 15 | 16 | scrawl: 17 | haxe -$(HAXELANG) SpiderScrawl.$(HAXELANG) -main cosmicos.SpiderScrawl 18 | 19 | ogham: 20 | haxe -$(HAXELANG) OghamStyle.$(HAXELANG) -main cosmicos.OghamStyle 21 | 22 | font: scrawl 23 | mkdir -p vectors 24 | NODE_PATH=$(PWD) node ./font/generate_glyphs.js 25 | cd vectors && ../font/glyphs_to_svg.sh 26 | cd font && fontcustom compile 27 | 28 | font-install: 29 | test -e $(COSMIC_SITE)/_config.yml 30 | cp -v font/app/assets/fonts/fontcustom_*.woff $(COSMIC_SITE)/fonts/cosmic_spider.woff 31 | cp -v font/app/assets/fonts/fontcustom_*.eot $(COSMIC_SITE)/fonts/cosmic_spider.eot 32 | cp -v font/app/assets/fonts/fontcustom_*.ttf $(COSMIC_SITE)/fonts/cosmic_spider.ttf 33 | cp -v font/app/assets/fonts/fontcustom_*.svg $(COSMIC_SITE)/fonts/cosmic_spider.svg 34 | 35 | glyph: 36 | NODE_PATH=$(PWD) node ./font/msg2glyphs.js ../msg/unwrapped.txt | grep "|" | sed "s| ../msg/glyph_head.txt 38 | 39 | glyph-install: 40 | test -e $(COSMIC_SITE)/_config.yml 41 | cp -v ../msg/glyph*.txt $(COSMIC_SITE)/_includes 42 | 43 | oghams: 44 | NODE_PATH=$(PWD) node ./font/msg2ogham.js ../msg/unwrapped.txt | grep "|" | sed "s| ../msg/glyph_head.txt 46 | 47 | 48 | yaml: 49 | # npm install -g json2yaml 50 | json2yaml assem2.json > assem2.yaml 51 | test -e $(COSMIC_SITE)/_config.yml 52 | cat $(COSMIC_SITE)/_config_base.yml assem2.yaml | sed "s/^---/msg:/" > $(COSMIC_SITE)/_config.yml 53 | 54 | primer: 55 | NODE_PATH=$(PWD) node ./assemble/primer.js assem.json 56 | 57 | console: 58 | echo "Do: cos = require('cos')" 59 | NODE_PATH=$(PWD):$(PWD)/assemble node 60 | 61 | 62 | test-eval: 63 | NODE_PATH=$(PWD) node ./tests/test_eval.js 64 | 65 | test: test-eval 66 | NODE_PATH=$(PWD) node ./tests/test_audio.js 67 | NODE_PATH=$(PWD) node ./tests/test_scrawl.js 68 | rm -f node.txt node.wav tests/test_scrawl.png 69 | 70 | -------------------------------------------------------------------------------- /src/assemble/assemble2json.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | var all = []; 4 | var cache = []; 5 | var expecting = /:/; 6 | var role = ""; 7 | var role_flush = ""; 8 | var coding = false; 9 | var markdown = false; 10 | 11 | function emit(txt) { 12 | // deal with markdown 13 | if (txt.indexOf('[[[') === 0) { 14 | markdown = true; 15 | all.push({ 16 | "role": role, 17 | "lines": cache 18 | }); 19 | cache = []; 20 | return; 21 | } 22 | if (txt.indexOf(']]]') === 0) { 23 | markdown = false; 24 | all.push({ 25 | "role": "doc", 26 | "lines": cache 27 | }); 28 | cache = []; 29 | return; 30 | } 31 | if (markdown) { 32 | cache.push(txt); 33 | return; 34 | } 35 | var need_flush = false; 36 | var blank = (!(/[^ \t]/.test(txt))); 37 | if (coding) blank = false; 38 | if (blank) { 39 | need_flush = true; 40 | role_flush = role; 41 | role = "code"; 42 | expecting = /:/; 43 | } else { 44 | var ch = (txt.length>0)?txt.charAt(0):' '; 45 | if (role=="code") { 46 | coding = !(/;/.test(txt)); 47 | } 48 | if (ch!=' '&&ch!='\t'&&!expecting.test(ch)) { 49 | need_flush = true; 50 | role_flush = role; 51 | role = "code"; 52 | } 53 | if (ch=='#') { 54 | expecting = /\#/; 55 | role = "comment"; 56 | } else if (ch === '~') { 57 | expecting = /~/; 58 | role = "doc"; 59 | } else if (ch=='>' && txt.length>=3 && txt.charAt(1)=='>' && txt.charAt(2)=='>') { 60 | expecting = /[>0-9]/; 61 | role = "gate"; 62 | } else { 63 | if (ch=='=' && txt.length>=3 && txt.charAt(1)=='=' && txt.charAt(2)==' ') { 64 | role = "file"; 65 | } 66 | if (need_flush) expecting = /:/; 67 | } 68 | } 69 | if (need_flush) { 70 | if (cache.length>0) { 71 | var jn = cache.join(" "); 72 | var len = jn.length; 73 | if (len>3) { 74 | if (jn.charAt(0)=="("&&jn.charAt(len-1)==";"&&jn.charAt(len-2)==")") { 75 | var at = 0; 76 | for (var k=1; k=0) { 83 | cache[0] = cache[0].substr(1); 84 | var alt = cache.length-1; 85 | cache[alt] = cache[alt].substr(0,cache[alt].length-2) + ";"; 86 | } 87 | if (jn.indexOf("point")>=0) { 88 | process.stderr.write(" " + at + " // " + jn + " --> " + cache.join(" ") + "\n"); 89 | } 90 | } 91 | } 92 | if (role_flush === 'doc') { 93 | if (cache.length >= 1) { 94 | if (cache[0].indexOf('~ ') === 0) { 95 | // markdown comment 96 | role_flush = "doc"; 97 | for (var i=0; i=0) { 23 | process.stderr.write("Skipping distill-circuit\n"); 24 | skippy = true; 25 | } 26 | if (op.indexOf("_harness")>=0) { 27 | process.stderr.write("Skipping _harness\n"); 28 | skippy = true; 29 | } 30 | if (op.indexOf("even-natural")>=0) { 31 | process.stderr.write("Skipping even-natural\n"); 32 | skippy = true; 33 | } 34 | } else { 35 | process.stderr.write("At " + i + "\n"); 36 | } 37 | 38 | var v = cosmicos.complete_stanza(part, !skippy); 39 | if (v!=1) { 40 | throw v; 41 | } 42 | } 43 | } catch (e) { 44 | process.stderr.write("* evaluate.js failed on " + err_i + ": " + JSON.stringify(err_part) + "\n"); 45 | console.error("Error", e); 46 | throw(e); 47 | } 48 | 49 | 50 | var ct = 0; 51 | for (var i=0; i>> ([_A-Z0-9]+)\.gate/; 67 | var part = all[i]; 68 | if (part.role != "gate") continue; 69 | if (part.lines.length==0) continue; 70 | var match = splitter.exec(part.lines[0]); 71 | if (match == null) continue; 72 | part["thumbnail"] = match[1] + ".gif"; 73 | part["page"] = match[1] + ".html"; 74 | } 75 | 76 | cosmicos.add_line_numbers(); 77 | 78 | var fs = require('fs'); 79 | fs.writeFileSync('q.txt', cosmicos.get_coded_message()); 80 | fs.writeFileSync('assem2.json',JSON.stringify(all, null, 2)); 81 | fs.writeFileSync('vocab.txt', JSON.stringify(cosmicos.get_vocab(), null, 2)); 82 | -------------------------------------------------------------------------------- /src/assemble/primer.js: -------------------------------------------------------------------------------- 1 | var CosmicDrive = require('CosmicDrive'); 2 | var cosmicos = new CosmicDrive({ 3 | 'primer': false, 4 | 'txt': false 5 | }); 6 | 7 | var all = cosmicos.get_message(); 8 | var primer = []; 9 | var primer2 = []; 10 | 11 | var cline = 0; 12 | for (var i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | static int content_mode = 1; 7 | 8 | void show(FILE *fout, int x, int n) { 9 | for (int i=0; i1) { factor = 1; } 66 | if (k!=4 && k!=5) { 67 | q += factor*100*sin((n)*v); 68 | if (n2>=0) { 69 | q += factor*20*sin((n2)*v); 70 | } 71 | if (chord) { 72 | //q += tweak*factor*12*sin((n*qminor)*v); 73 | //q += tweak*factor*12*sin((n/qminor)*v); 74 | } else { 75 | } 76 | } 77 | if (k_prev!=4 && k_prev!=5) { 78 | if (i!=0) { 79 | q += (1-factor)*100*sin((n_prev)*v); 80 | } 81 | } 82 | if (n2_prev>=0) { 83 | q += (1-factor)*20*sin((n2_prev)*v); 84 | } 85 | if (k==4 || k==5) { 86 | if (k==4) { 87 | q += tweak*factor*50*sin(base*v); 88 | q += tweak*factor*25*sin(2*base*v); 89 | } else { 90 | q += tweak*factor*50*sin(base*v); 91 | q += tweak*factor*25*sin(2*base*v); 92 | q += tweak*factor*12*sin(4*base*v); 93 | q += tweak*factor*12*sin(8*base*v); 94 | } 95 | } 96 | show(fout,128+int(q),1); 97 | //show(fout,128+int(127*sin(i*0.1)),1); 98 | v += 0.1; 99 | } 100 | n_prev = n; 101 | k_prev = k; 102 | n2_prev = n2; 103 | } 104 | } 105 | 106 | 107 | 108 | int main(int argc, char *argv[]) { 109 | FILE *fout = stdout; 110 | char *default_text = "01234543210"; 111 | char *text = default_text; 112 | argc--; 113 | argv++; 114 | while (argc>0) { 115 | text = argv[0]; 116 | if (text[0]=='-') { 117 | switch (text[1]) { 118 | case 'w': 119 | content_mode = 0; 120 | break; 121 | } 122 | } 123 | argc--; 124 | argv++; 125 | } 126 | if (content_mode) { 127 | text = default_text; 128 | char *data; 129 | data = getenv("QUERY_STRING"); 130 | if(data != NULL) { 131 | if (data[0]=='s') { 132 | if (data[1]=='=') { 133 | text = data+2; 134 | } 135 | } 136 | } 137 | 138 | printf("Content-Type: audio/x-wav"); 139 | printf("%c%c",10,10); 140 | } 141 | render(fout,text); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /src/cmake/assemble.cmake: -------------------------------------------------------------------------------- 1 | separate_arguments(in) 2 | file(WRITE ${out} "") 3 | foreach(f ${in}) 4 | file(APPEND ${out} "== ${f} ==\n") 5 | if (EXISTS ${f}_md.ftz) 6 | file(READ ${f}_md.ftz CONTENTS) 7 | file(APPEND ${out} "${CONTENTS}") 8 | endif() 9 | file(READ ${f}.ftz CONTENTS) 10 | file(APPEND ${out} "${CONTENTS}") 11 | endforeach() 12 | -------------------------------------------------------------------------------- /src/cmake/convert_gate.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | 3 | execute_process(COMMAND ${JAVA_EXE} -cp "${UNLESS}" UnlessDriver ${in} OUTPUT_FILE ${out_dir}/${out}.base WORKING_DIRECTORY ${base}) 4 | execute_process(COMMAND perl ${base}/../src/filter/drawgate-ppm.pl INPUT_FILE ${out}.base OUTPUT_FILE ${name}.ppm) 5 | execute_process(COMMAND perl ${base}/../src/filter/drawgate-txt.pl INPUT_FILE ${out}.base OUTPUT_FILE ${out}.text) 6 | 7 | file(READ ${out}.text CONTENTS) 8 | string(TOLOWER "${name}" name_lc) 9 | string(REGEX REPLACE "CIRCUIT_NAME" "${name_lc}" CONTENTS "${CONTENTS}") 10 | file(WRITE ${out} "${CONTENTS}") 11 | file(READ ${out}.base CONTENTS) 12 | file(APPEND ${out} "${CONTENTS}") 13 | -------------------------------------------------------------------------------- /src/cmake/convert_java.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | 3 | file(READ ${base}/${in} CONTENTS) 4 | if("${CONTENTS}" MATCHES "STUB: ([^*]+) \\*") 5 | file(WRITE ${out} "${CMAKE_MATCH_1}\n") 6 | else() 7 | execute_process(COMMAND ${JAVAC_EXE} -source 6 -cp "." -d ${out_dir} ${in} WORKING_DIRECTORY ${base}) 8 | execute_process(COMMAND ${JAVA_EXE} -cp ".:${BCEL}:${ENCODER}" Fritzifier ${name} ${out}.text WORKING_DIRECTORY ${out_dir}) 9 | execute_process(COMMAND perl ${base}/../src/filter/java-comment.pl ${out}.text ${base}/${name}.java OUTPUT_FILE ${out}) 10 | endif() 11 | -------------------------------------------------------------------------------- /src/cmake/convert_js.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | set(ENV{NODE_PATH} ${base}) 3 | execute_process(COMMAND node ${base}/${in} OUTPUT_FILE ${out} RESULT_VARIABLE result) 4 | 5 | if (NOT ${result} EQUAL 0) 6 | if (EXISTS ${out}) 7 | file(REMOVE ${out}) 8 | endif() 9 | message(FATAL_ERROR "node returned ${result}") 10 | endif() -------------------------------------------------------------------------------- /src/cmake/convert_md.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | file(READ ${base}/${in} FIN) 3 | file(WRITE ${out} "[[[\n") 4 | file(APPEND ${out} ${FIN}) 5 | file(APPEND ${out} "\n]]]\n") 6 | 7 | #execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${base}/${in} ${out}) 8 | -------------------------------------------------------------------------------- /src/cmake/convert_pl.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | execute_process(COMMAND perl -I${base} ${base}/${in} OUTPUT_FILE ${out} RESULT_VARIABLE result) 3 | 4 | if (NOT ${result} EQUAL 0) 5 | if (EXISTS ${out}) 6 | file(REMOVE ${out}) 7 | endif() 8 | message(FATAL_ERROR "Error processing ${in}: perl returned ${result}") 9 | endif() 10 | -------------------------------------------------------------------------------- /src/cmake/convert_scm.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${base}/${in} ${out}) 3 | -------------------------------------------------------------------------------- /src/cmake/node.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | set(ENV{NODE_PATH} "${out_dir}:${path}:$ENV{NODE_PATH}:${CMAKE_BINARY_DIR}/../js") 3 | execute_process(COMMAND ${NODE} ${in} ${nargs} OUTPUT_FILE ${out_dir}/${log}.txt) 4 | -------------------------------------------------------------------------------- /src/cmake/typescript.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | set(ENV{NODE_PATH} "${out_dir}:${path}:$ENV{NODE_PATH}") 3 | execute_process(COMMAND ${TSNODE} ${in} ${nargs} OUTPUT_FILE ${out_dir}/${log}.txt) 4 | -------------------------------------------------------------------------------- /src/cmake/wc.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | execute_process(COMMAND perl -e "print(length(join('',<>)),'\n')" ${in} OUTPUT_FILE ${out} RESULT_VARIABLE result) 3 | 4 | if (NOT ${result} EQUAL 0) 5 | if (EXISTS ${out}) 6 | file(REMOVE ${out}) 7 | endif() 8 | message(FATAL_ERROR "wc.cmake returned ${result}") 9 | endif() 10 | -------------------------------------------------------------------------------- /src/cmake/wrap.cmake: -------------------------------------------------------------------------------- 1 | include(setup.cmake) 2 | execute_process(COMMAND perl ${base}/../src/filter/wrap.pl ${in} OUTPUT_FILE ${out} RESULT_VARIABLE result) 3 | 4 | if (NOT ${result} EQUAL 0) 5 | if (EXISTS ${out}) 6 | file(REMOVE ${out}) 7 | endif() 8 | message(FATAL_ERROR "wrap.cmake returned ${result}") 9 | endif() 10 | -------------------------------------------------------------------------------- /src/cmd/cosh.ts: -------------------------------------------------------------------------------- 1 | import Eval from './Eval'; 2 | 3 | const evaluator = new Eval(); 4 | let server: any; 5 | 6 | function cosmicosEval(input: string, context: unknown, filename: string, 7 | callback: (x: null, y: any) => void) { 8 | const result = evaluator.apply(input, console.log); 9 | if (result !== undefined) { 10 | if (result !== null) { 11 | callback(null, result); 12 | } else { 13 | server.displayPrompt(); 14 | } 15 | } 16 | } 17 | 18 | const args = process.argv.slice(2); 19 | 20 | if (args.length == 0) { 21 | console.log("Welcome to a CosmicOS test console, \"help\" and \"examples\" available"); 22 | const repl = require('repl'); 23 | server = repl.start({ 24 | prompt: "cosmicos> ", 25 | input: process.stdin, 26 | output: process.stdout, 27 | eval: cosmicosEval 28 | }); 29 | } else if (args[0] == "-c") { 30 | console.log(evaluator.apply(args[1], console.log)); 31 | } else { 32 | console.error("huh?"); 33 | } 34 | -------------------------------------------------------------------------------- /src/cmd/cosmsg.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require('path'); 4 | 5 | var binaryDir = path.resolve(__dirname, '..'); 6 | var delta = path.relative('@CMAKE_BINARY_DIR@', '@CMAKE_SOURCE_DIR@'); 7 | var sourceDir = path.resolve(binaryDir, delta); 8 | 9 | var cos = require(path.resolve(sourceDir, 'src/assemble/cosmicos.js')); 10 | return cos(binaryDir,sourceDir); 11 | -------------------------------------------------------------------------------- /src/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "variant": "@COSMIC_VARIANT@", 3 | "use_flattener": @COSMIC_USE_FLATTENER@, 4 | "lines": @COSMIC_LINES@, 5 | "vocab": "@COSMIC_VOCAB@", 6 | "names": "@COSMIC_NAMES@" 7 | } 8 | 9 | -------------------------------------------------------------------------------- /src/cosmicos/AllNumericCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | public ParseCodec implements Codec { 7 | private var vocab : Vocab; 8 | 9 | public function new(vocab: Vocab) { 10 | this.vocab = vocab; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | var txt : String = cast src.content[0]; 15 | src.content = Parse.stringToList(txt); 16 | return true; 17 | } 18 | 19 | public function decode(src: Statement) : Bool { 20 | return true; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/cosmicos/BitString.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | // temporary workaround to support old lesson, this should be going away soon 6 | @:expose 7 | class BitString { 8 | public var txt : String; 9 | public function new(txt: String) { 10 | this.txt = txt; 11 | } 12 | 13 | public function asBigInteger() : BigInteger { 14 | var u : BigInteger = BigInteger.ofInt(0); 15 | var two : BigInteger = BigInteger.ofInt(2); 16 | for (j in 0...txt.length) { 17 | u = u.mul(two); 18 | if (txt.charAt(j) == ':') u = u.add(BigInteger.ONE); 19 | } 20 | return u; 21 | } 22 | 23 | public function small() : Bool { 24 | return txt.length < 15; 25 | } 26 | 27 | public function asInteger() : Dynamic { 28 | if (!small()) return asBigInteger(); 29 | var u : Int = 0; 30 | for (j in 0...txt.length) { 31 | u *= 2; 32 | if (txt.charAt(j) == ':') u++; 33 | } 34 | return u; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/cosmicos/CacheCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class CacheCodec implements Codec { 7 | private var codec : Codec; 8 | public var cache : Statement; 9 | 10 | public function new(codec : Codec) { 11 | this.codec = codec; 12 | this.cache = null; 13 | } 14 | 15 | public function get() : Statement { 16 | return cache; 17 | } 18 | 19 | public function encode(src: Statement) : Bool { 20 | var result = codec.encode(); 21 | cache = src.copy(); 22 | return result; 23 | } 24 | 25 | public function decode(src: Statement) : Bool { 26 | cache = src.copy(); 27 | return codec.decode(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/cosmicos/ChainCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class ChainCodec implements Codec { 7 | private var codecs : Array; 8 | 9 | public function new(codecs : Array) { 10 | this.codecs = codecs; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | for (i in 0...codecs.length) { 15 | var result = codecs[i].encode(src); 16 | if (!result) return result; 17 | } 18 | return true; 19 | } 20 | 21 | public function decode(src: Statement) : Bool { 22 | for (i in 0...codecs.length) { 23 | var result = codecs[codecs.length - i - 1].decode(src); 24 | if (!result) return result; 25 | } 26 | return true; 27 | } 28 | 29 | public function last() : Codec { 30 | if (codecs.length == 0) return null; 31 | return codecs[codecs.length - 1]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/cosmicos/Codec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | interface Codec { 7 | function encode(src: Statement) : Bool; 8 | 9 | function decode(src: Statement) : Bool; 10 | } 11 | -------------------------------------------------------------------------------- /src/cosmicos/Codec.ts: -------------------------------------------------------------------------------- 1 | import {Statement} from './Statement'; 2 | 3 | export interface Codec { 4 | encode(src: Statement): boolean; 5 | decode(src: Statement): boolean; 6 | } 7 | -------------------------------------------------------------------------------- /src/cosmicos/Complex.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Complex { 7 | public var re:Float; 8 | public var im:Float; 9 | 10 | public function new(re: Float = 0, im: Float = 0) { 11 | this.re = re; 12 | this.im = im; 13 | } 14 | 15 | public function toString() { 16 | return "[" + re + ", " + im + "]"; 17 | } 18 | 19 | inline public function clone() { 20 | return new Complex(re, im); 21 | } 22 | 23 | public function mul(alt: Complex) { 24 | return new Complex(re * alt.re - im * alt.im, re * alt.im + alt.re * im); 25 | } 26 | 27 | public function div(alt: Complex) { 28 | return new Complex((re*alt.re+im*alt.im)/(alt.re*alt.re+alt.im*alt.im), 29 | (im*alt.re-re*alt.im)/(alt.re*alt.re+alt.im*alt.im)); 30 | } 31 | 32 | public function add(alt: Complex) { 33 | return new Complex(re + alt.re, im + alt.im); 34 | } 35 | 36 | public function sub(alt: Complex) { 37 | return new Complex(re - alt.re, im - alt.im); 38 | } 39 | 40 | public inline function equals(c2:Complex) { 41 | return floatEquals(c2.re, re) && floatEquals(c2.im, im); 42 | } 43 | 44 | static public function floatEquals(lhs:Float, rhs:Float) { 45 | return Math.abs(lhs - rhs) < 0.00000001; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/cosmicos/Config.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Config { 7 | private var config : Dynamic; 8 | private var external_vocab : Dynamic; 9 | 10 | public function new(txt: String = null) { 11 | config = null; 12 | external_vocab = null; 13 | if (txt != null) { 14 | config = haxe.Json.parse(txt); 15 | } 16 | } 17 | 18 | /** 19 | * 20 | * Option: is flattener syntax "|" and "$" supported in the message or should 21 | * these be mapped to parentheses. 22 | * 23 | */ 24 | public function useFlattener() : Bool { 25 | if (config == null) return true; 26 | return Reflect.field(config, 'use_flattener'); 27 | } 28 | 29 | /** 30 | * 31 | * 32 | * Number of lines of message to work on - 0 means unlimited. Can be convenient 33 | * to set to a small number during development. 34 | * 35 | */ 36 | public function lines() : Int { 37 | if (config == null) return 0; 38 | return Reflect.field(config, 'lines'); 39 | } 40 | 41 | public function getExternalVocabPath() : String { 42 | if (config == null) return null; 43 | var fname = Reflect.field(config, 'vocab'); 44 | if (fname == "") return null; 45 | return fname; 46 | } 47 | 48 | public function getNamesPath() : String { 49 | if (config == null) return null; 50 | var fname = Reflect.field(config, 'names'); 51 | if (fname == "") return null; 52 | return fname; 53 | } 54 | 55 | public function setExternalVocab(txt : String) { 56 | external_vocab = haxe.Json.parse(txt); 57 | } 58 | 59 | public function getExternalVocab() : Dynamic { 60 | return external_vocab; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/cosmicos/Cons.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Cons { 7 | 8 | public static function cons(x:Dynamic,y:Dynamic) { 9 | return function(f) { return f(x)(y); }; 10 | } 11 | 12 | public static function car(x:Dynamic) { 13 | return x(function(a) { return function(b) { return a; }}); 14 | } 15 | 16 | public static function cdr(x:Dynamic) { 17 | return x(function(a) { return function(b) { return b; }}); 18 | } 19 | 20 | public static function deconsify(e: Dynamic) : Dynamic { 21 | if (Std.is(e,Int)) return e; 22 | if (Std.is(e,BigInteger)) return e; 23 | if (Std.is(e,String)) return e; 24 | if (Std.is(e,BitString)) return e; 25 | if (Std.is(e,Float)) return e; 26 | var c = new Cursor(e); 27 | var lst = new Array(); 28 | var len = c.length(); 29 | for (i in 0...len) { 30 | var ei = c.next(); 31 | lst.push(deconsify(ei)); 32 | } 33 | return lst; 34 | } 35 | 36 | public static function consify(e: Dynamic) : Dynamic { 37 | if (!Std.is(e,Array)) return e; 38 | var lst : Array = cast e; 39 | var len : Int = lst.length; 40 | if (len==0) return cons(0,0); 41 | if (len==1) return cons(1,consify(lst[0])); 42 | var r = cons(consify(lst[len-2]),consify(lst[len-1])); 43 | for (i in 0...(len-2)) { 44 | r = cons(consify(lst[len-3-i]),r); 45 | } 46 | return cons(len,r); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/cosmicos/CosDefine.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class CosDefine { 7 | public var k : Dynamic; 8 | public var v : Dynamic; 9 | 10 | public function new(k: Dynamic, v: Dynamic) { 11 | this.k = k; 12 | this.v = v; 13 | } 14 | } -------------------------------------------------------------------------------- /src/cosmicos/CosFunction.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class CosFunction { 7 | public var fn : Dynamic; 8 | public var meta : Bool; 9 | 10 | public function new(fn: Dynamic, meta: Bool) { 11 | this.fn = fn; 12 | this.meta = meta; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/cosmicos/Cursor.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | class Cursor { 6 | public var at : Int; 7 | public var e : Dynamic; 8 | public var len : Int; 9 | 10 | public function new(e: Dynamic) { 11 | at = 0; 12 | len = Cons.car(e); 13 | this.e = Cons.cdr(e); 14 | } 15 | 16 | public function length() : Int { 17 | return len; 18 | } 19 | 20 | public function next() : Dynamic { 21 | // Expression-as-list is faster, but consing needed for 22 | // replaceability of translate function 23 | //var lst : Array = cast e; 24 | //var result = lst[at]; 25 | var result = null; 26 | if (len==1) { 27 | result = e; 28 | e = null; 29 | } else if (at==len-1) { 30 | result = Cons.cdr(e); 31 | e = null; 32 | } else { 33 | result = Cons.car(e); 34 | if (at!=len-2) e = Cons.cdr(e); 35 | } 36 | at++; 37 | return result; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/cosmicos/EvaluateCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class EvaluateCodec implements Codec { 7 | public var state : State; 8 | public var eval : Evaluate; 9 | 10 | public function new(state : State = null, with_std_functions : Bool = true) { 11 | this.state = state; 12 | eval = new Evaluate(state); 13 | eval.applyOldOrder(); 14 | if (with_std_functions) { 15 | eval.addStdMin(); 16 | } 17 | } 18 | 19 | public function addPrimer(primer : Dynamic) { 20 | eval.addPrimer(primer); 21 | } 22 | 23 | public function encode(src: Statement) : Bool { 24 | src.content = [eval.evaluateExpression(src.content)]; 25 | return true; 26 | } 27 | 28 | public function decode(src: Statement) : Bool { 29 | src.content = src.content[0]; 30 | var r = Cons.deconsify(src.content); 31 | var vocab = state.getVocab(); 32 | if (vocab.exists("vector")) { 33 | var vector = vocab.get("vector"); 34 | vectorify(r, vector); 35 | } 36 | src.content = Cons.consify(r); 37 | return true; 38 | } 39 | 40 | public function vectorify(v: Dynamic, vector: Dynamic) { 41 | if (Std.is(v, Array)) { 42 | var ei : Array = cast v; 43 | for (i in 0...ei.length) { 44 | ei[i] = vectorify(ei[i], vector); 45 | } 46 | ei.insert(0, vector); 47 | return ei; 48 | } else { 49 | return v; 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/cosmicos/ExternalVocab.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class ExternalVocab { 7 | private var config : Config; 8 | 9 | public function new(config : Config) { 10 | this.config = config; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/cosmicos/FourSymbolCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class FourSymbolCodec implements Codec { 7 | public var vocab : Vocab; 8 | 9 | public function new(vocab : Vocab) { 10 | this.vocab = vocab; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | var txt = Parse.codify(src.content, vocab); 15 | src.content = [txt]; 16 | return true; 17 | } 18 | 19 | public function decode(src: Statement) : Bool { 20 | var txt = src.content[0]; 21 | var out = ""; 22 | var len = txt.length; 23 | var unit = ""; 24 | for (i in 0...len) { 25 | var ch = txt.charAt(i); 26 | if (ch == '0') { 27 | unit += "."; 28 | } else if (ch == '1') { 29 | unit += ":"; 30 | } else if (ch == '2') { 31 | out += unit; 32 | unit = ""; 33 | out += "("; 34 | } else if (ch == '3') { 35 | if (unit != "") { 36 | var bs = new BitString(unit); 37 | if (bs.small()) { 38 | out += "]"; 39 | out += bs.asInteger(); 40 | out += "["; 41 | unit = ""; 42 | } else { 43 | out += " "; 44 | out += unit; 45 | out += " "; 46 | unit = ""; 47 | } 48 | } 49 | out += ")"; 50 | } 51 | } 52 | var r = ~/\(\]/g; 53 | var out = r.replace(out, " "); 54 | r = ~/\[\)/g; 55 | out = r.replace(out, " "); 56 | r = ~/:\(\)/g; 57 | out = r.replace(out, " | "); 58 | r = ~/\.\(\) */g; 59 | out = r.replace(out, " $"); 60 | r = ~/ *\(\(\)\)/g; 61 | out = r.replace(out, ";"); 62 | r = ~/ */g; 63 | out = r.replace(out, " "); 64 | var codec = new cosmicos.ChainCodec([ 65 | new cosmicos.ParseCodec(vocab), 66 | new cosmicos.NormalizeCodec(vocab) 67 | ]); 68 | var dest = new cosmicos.Statement(out); 69 | codec.encode(dest); 70 | src.content = dest.content; 71 | return true; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/cosmicos/HumanSymbolCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class HumanSymbolCodec implements Codec { 7 | public var config : Config; 8 | 9 | public function new(config : Config) { 10 | this.config = config; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | return true; 15 | } 16 | 17 | public function decode(src: Statement) : Bool { 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/cosmicos/ManuscriptStyle.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class ManuscriptStyle { 7 | 8 | private var out : Array; 9 | private var need_ws : Bool; 10 | 11 | public function new() { 12 | } 13 | 14 | public function render(x: Array) : Array { 15 | out = new Array(); 16 | need_ws = false; 17 | renderNest(x,false); 18 | return out; 19 | } 20 | 21 | public function ws() { 22 | if (need_ws) { 23 | out.push(" "); 24 | need_ws = false; 25 | } 26 | } 27 | 28 | public function nws() { 29 | need_ws = true; 30 | } 31 | 32 | public function renderInt(x: Int) { 33 | ws(); 34 | out.push(x); 35 | nws(); 36 | } 37 | 38 | public function renderString(x: String) { 39 | ws(); 40 | if (x=="") { 41 | out.push(""); 42 | return; 43 | } 44 | /* 45 | var lst = x.split(":"); 46 | for (e in lst) { 47 | out.push(e); 48 | } 49 | */ 50 | out.push(x); 51 | nws(); 52 | } 53 | 54 | public function renderNest(x: Array, nested: Bool) { 55 | var offset = 0; 56 | var parens = nested; 57 | if (x.length>=1) { 58 | var e = x[0]; 59 | if (e==-1||e==-2) { 60 | offset = 1; 61 | if (e==-1) { 62 | ws(); 63 | out.push("|"); 64 | nws(); 65 | } 66 | if (e==-2) { 67 | ws(); 68 | out.push("$"); 69 | } 70 | parens = false; 71 | } 72 | } 73 | if (parens) out.push("("); 74 | for (i in offset...x.length) { 75 | var e = x[i]; 76 | if (Std.is(e,String)) { 77 | renderString(cast e); 78 | } 79 | if (Std.is(e,Int)) { 80 | renderInt(cast e); 81 | } 82 | if (Std.is(e,Array)) { 83 | ws(); 84 | renderNest(cast e,true); 85 | nws(); 86 | } 87 | } 88 | if (parens) out.push(")"); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/cosmicos/Memory.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Memory { 7 | public var parent : Memory; 8 | public var block : Map; 9 | public var key : Dynamic; 10 | public var val : Dynamic; 11 | 12 | public function new(parent: Memory, key : Dynamic = -1, val : Dynamic = null) { 13 | this.parent = parent; 14 | this.key = key; 15 | this.val = val; 16 | if (key==-1) { 17 | block = new Map(); 18 | } 19 | } 20 | 21 | public function add(key: Dynamic, val: Dynamic) { 22 | if (block!=null) { 23 | block.set(key,val); 24 | return; 25 | } 26 | if (parent!=null) { 27 | parent.add(key,val); 28 | } 29 | } 30 | 31 | public function get(key: Dynamic) : Dynamic { 32 | if (block==null) { 33 | if (this.key==key) return val; 34 | if (parent==null) return null; 35 | return parent.get(key); 36 | } 37 | var result = block.get(key); 38 | if (result==null && parent!=null) result = parent.get(key); 39 | return result; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/cosmicos/NormalizeCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class NormalizeCodec implements Codec { 7 | private var vocab : Vocab; 8 | 9 | public function new(vocab: Vocab) { 10 | this.vocab = vocab; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | Parse.encodeSymbols(src.content, vocab); 15 | return true; 16 | } 17 | 18 | public function decode(src: Statement) : Bool { 19 | // Decoded version may not match encoded version, but should 20 | // evaluate to same thing. 21 | src.content = recover(src.content, 0); 22 | return true; 23 | } 24 | 25 | private function recover(v: Dynamic, level: Int) : Dynamic { 26 | if (Std.is(v, Array)) { 27 | var ei : Array = cast v; 28 | for (i in 0...ei.length) { 29 | ei[i] = recover(ei[i], level + 1); 30 | } 31 | return ei; 32 | } else { 33 | return Parse.recover(v); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/cosmicos/ParseCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class ParseCodec implements Codec { 7 | private var vocab : Vocab; 8 | private var top : Bool; 9 | 10 | public function new(vocab: Vocab, top=true) { 11 | this.vocab = vocab; 12 | this.top = top; 13 | } 14 | 15 | public function encode(src: Statement) : Bool { 16 | var txt : String = cast src.content[0]; 17 | src.content = Parse.stringToList(txt, vocab); 18 | return true; 19 | } 20 | 21 | public function decode(src: Statement) : Bool { 22 | // Decoded version may not match encoded version, but should 23 | // evaluate to same thing. 24 | if (top) { 25 | src.content = [flatten(src.content, 0) + ";"]; 26 | } else { 27 | src.content = [flatten(src.content, 1)]; 28 | } 29 | return true; 30 | } 31 | 32 | private function flatten(v: Dynamic, level: Int) : String { 33 | if (Std.is(v, Array)) { 34 | var ei : Array = cast v; 35 | var txts : Array = []; 36 | var len : Int = ei.length; 37 | var has_flattener : Bool = false; 38 | var flattener : Int = 0; 39 | for (i in 0...len) { 40 | if (i == 0 && ei[i] < 0) { 41 | has_flattener = true; 42 | flattener = ei[i]; 43 | continue; 44 | } 45 | txts.push(flatten(ei[i], level + 1)); 46 | } 47 | var result : String = txts.join(" "); 48 | if (has_flattener) { 49 | if (flattener == -2) { 50 | result = "$" + result; 51 | } else { 52 | result = "| " + result; 53 | } 54 | } else { 55 | if (level > 0) result = "(" + result + ")"; 56 | } 57 | return result; 58 | } 59 | return Std.string(v); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/cosmicos/PreprocessCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class PreprocessCodec implements Codec { 7 | public var config : Config; 8 | 9 | public function new(state : State) { 10 | this.config = state.getConfig(); 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | if (!config.useFlattener()) { 15 | src.content[0] = Parse.removeFlatteningSyntax(src.content[0]); 16 | } 17 | return true; 18 | } 19 | 20 | public function decode(src: Statement) : Bool { 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/cosmicos/Rename.ts: -------------------------------------------------------------------------------- 1 | 2 | import * as standardNames from '../../msg/names.json'; 3 | 4 | export class Rename { 5 | private names = new Map(); 6 | private unnames = new Map(); 7 | 8 | public constructor(pairs: Array<[string, string]> = []) { 9 | if (pairs.length === 0) { 10 | pairs = standardNames as any; 11 | } 12 | for (const pair of pairs) { 13 | if (pair[0]) { 14 | this.add(pair[0], pair[1]); 15 | } 16 | } 17 | } 18 | 19 | public add(src: string, dest: string) { 20 | this.names.set(src, dest); 21 | this.unnames.set(dest, src); 22 | } 23 | 24 | public get(src: string): string { 25 | if (src.charAt(0) === ':') { return src; } 26 | if (this.names.has(src)) { 27 | return this.names.get(src); 28 | } 29 | const parts = src.split(':'); 30 | const next = parts.length > 1 ? parts.map(part => this.get(part)).join(':') : src; 31 | if (next === src) { return next; } 32 | return this.get(next); 33 | } 34 | 35 | // this is weak, doesn't deal with parts. 36 | public unget(src: string): string { 37 | while (this.unnames.has(src)) { 38 | src = this.unnames.get(src); 39 | } 40 | return src; 41 | } 42 | 43 | public rename(e: any[]) { 44 | for (let i = 0; i < e.length; i++) { 45 | const v = e[i]; 46 | if (Array.isArray(v)) { 47 | this.rename(v); 48 | continue; 49 | } 50 | if (!(typeof v === 'string')) { continue; } 51 | e[i] = this.get(e[i]); 52 | } 53 | return e; 54 | } 55 | 56 | public renameWithinString(src: string): string { 57 | const parts: string[] = []; 58 | let base: number = 0; 59 | let prevActive: boolean = false; 60 | const letter = new RegExp(/[-a-zA-Z0-9.:!]/); 61 | for (let i = 0; i <= src.length; i++) { 62 | const active = (i < src.length) ? letter.test(src.charAt(i)) : !prevActive; 63 | if (active !== prevActive) { 64 | if (i !== base) { 65 | const part = src.substr(base, i - base); 66 | base = i; 67 | parts.push(prevActive ? this.get(part) : part); 68 | } 69 | } 70 | prevActive = active; 71 | } 72 | return parts.join(''); 73 | } 74 | 75 | public unrenameWithinString(src: string): string { 76 | const parts: string[] = []; 77 | let base: number = 0; 78 | let prevActive: boolean = false; 79 | const letter = new RegExp(/[-a-zA-Z0-9.:!]/); 80 | for (let i = 0; i <= src.length; i++) { 81 | const active = (i < src.length) ? letter.test(src.charAt(i)) : !prevActive; 82 | if (active !== prevActive) { 83 | if (i !== base) { 84 | const part = src.substr(base, i - base); 85 | base = i; 86 | parts.push(prevActive ? this.unget(part) : part); 87 | } 88 | } 89 | prevActive = active; 90 | } 91 | return parts.join(''); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/cosmicos/SpiderImage.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | interface SpiderImage { 7 | 8 | function moveTo( x : Float, y : Float ) : Void; 9 | function lineTo( x : Float, y : Float ) : Void; 10 | function beginPath() : Void; 11 | function stroke() : Void; 12 | } 13 | -------------------------------------------------------------------------------- /src/cosmicos/State.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class State { 7 | private var config : Config; 8 | private var vocab : Vocab; 9 | private var mem : Memory; 10 | 11 | public function new(config : Config = null) { 12 | this.config = config; 13 | vocab = null; 14 | mem = null; 15 | } 16 | 17 | public function setConfig(config : Config) { 18 | this.config = config; 19 | } 20 | 21 | public function useIntVocab() { 22 | vocab = new Vocab(true); 23 | } 24 | 25 | public function getVocab() : Vocab { 26 | if (vocab == null) vocab = new Vocab(); 27 | return vocab; 28 | } 29 | 30 | public function getMemory() : Memory { 31 | if (mem == null) mem = new Memory(null); 32 | return mem; 33 | } 34 | 35 | public function getConfig() : Config { 36 | if (config == null) config = new Config(); 37 | return config; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/cosmicos/Statement.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Statement { 7 | public var content : Array; 8 | 9 | public function new(txt: Dynamic = null) { 10 | if (txt != null) { 11 | this.content = [txt]; 12 | } else { 13 | this.content = null; 14 | } 15 | } 16 | 17 | public function copy() : Statement { 18 | var result = new Statement(); 19 | if (this.content != null) { 20 | result.content = copyArray(this.content); 21 | } 22 | return result; 23 | } 24 | 25 | private function copyArray(e : Array) : Array { 26 | var result = new Array(); 27 | for (i in 0...e.length) { 28 | var ei = e[i]; 29 | if (Std.is(ei, Array)) { 30 | result.push(copyArray(cast ei)); 31 | } else { 32 | result.push(ei); 33 | } 34 | } 35 | return result; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/cosmicos/Statement.ts: -------------------------------------------------------------------------------- 1 | export interface Statement { 2 | content: any[]; 3 | } 4 | 5 | export class WideStatement implements Statement { 6 | public constructor(public content: any[]) { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/cosmicos/TranslateCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class TranslateCodec implements Codec { 7 | public var state : State; 8 | 9 | public function new(state : State) { 10 | this.state = state; 11 | } 12 | 13 | public function encode(src: Statement) : Bool { 14 | src.content = Cons.consify(src.content); 15 | var vocab = state.getVocab(); 16 | if (vocab.exists("translate")) { 17 | var mem = state.getMemory(); 18 | var translate = mem.get(vocab.get("translate")); 19 | if (translate!=null) { 20 | src.content = translate(src.content); 21 | } 22 | } 23 | return true; 24 | } 25 | 26 | public function decode(src: Statement) : Bool { 27 | // Cannot undo the translation, but can undo consification 28 | src.content = Cons.deconsify(src.content); 29 | trace("Translated"); 30 | trace(src.content); 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/cosmicos/UnflattenCodec.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class UnflattenCodec implements Codec { 7 | 8 | public function new() { 9 | } 10 | 11 | public function encode(src: Statement) : Bool { 12 | Parse.removeSlashMarker(src.content); 13 | return true; 14 | } 15 | 16 | public function decode(src: Statement) : Bool { 17 | // Decoded version may not match encoded version, but should 18 | // evaluate to same thing. 19 | unflatten(src.content, 0); 20 | return true; 21 | } 22 | 23 | private function unflatten(v: Dynamic, level: Int) : Void { 24 | if (Std.is(v, Array)) { 25 | var ei : Array = cast v; 26 | var txts : Array = []; 27 | var len : Int = ei.length; 28 | var has_flattener : Bool = false; 29 | for (i in 0...len) { 30 | unflatten(ei[i], level + 1); 31 | if (i == len - 1 && Std.is(ei[i], Array)) { 32 | var etail : Array = cast ei[i]; 33 | if (etail.length > 0) { 34 | if (etail[0] != -1 && etail[0] != -2) { 35 | etail.insert(0, -1); 36 | } 37 | } 38 | } 39 | } 40 | if (len == 1) { 41 | ei.insert(0, -2); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/cosmicos/Vocab.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | @:expose 6 | class Vocab { 7 | private var nameToCode : Map; 8 | private var codeToName : Map; 9 | private var nameToMeta : Map; 10 | private var used: Map; 11 | private var topCode : Int; 12 | private var useInt : Bool; 13 | 14 | public function new(useInt : Bool = false) { 15 | clear(); 16 | this.useInt = useInt; 17 | } 18 | 19 | public function clear() { 20 | nameToCode = new Map(); 21 | codeToName = new Map(); 22 | nameToMeta = new Map(); 23 | used = new Map(); 24 | topCode = 0; 25 | } 26 | 27 | public function getBase(name: String) : Int { 28 | if (name=="define") name = "@"; 29 | if (!nameToCode.exists(name)) { 30 | while (codeToName.exists(topCode)) { 31 | topCode++; 32 | } 33 | nameToCode.set(name,topCode); 34 | codeToName.set(topCode,name); 35 | topCode++; 36 | } 37 | return nameToCode.get(name); 38 | } 39 | 40 | public function use(name: String) { 41 | used.set(name, 1); 42 | } 43 | 44 | public function get(name: String) : Dynamic { 45 | if (name=="define") name = "@"; 46 | var code = getBase(name); // keep allocating ints for now 47 | return useInt ? code : name; 48 | } 49 | 50 | public function getInt(name: String) : Dynamic { 51 | if (name=="define") name = "@"; 52 | var code = getBase(name); // keep allocating ints for now 53 | return code; 54 | } 55 | 56 | public function exists(name: String) : Bool { 57 | return nameToCode.exists(name); 58 | } 59 | 60 | public function check(name: String, id : Int) : Dynamic { 61 | if (nameToCode.exists(name)) { 62 | var nid : Int = getBase(name); 63 | if (id!=nid) { 64 | throw("id for " + name + " is unexpected (" + nid + " vs " + id + ")"); 65 | } 66 | } else if (codeToName.exists(id)) { 67 | throw("problem with " + name); 68 | } else { 69 | set(name, id); 70 | } 71 | return get(name); 72 | } 73 | 74 | public function set(name: String, id : Int) : String { 75 | codeToName.set(id,name); 76 | nameToCode.set(name,id); 77 | return get(name); 78 | } 79 | 80 | public function setMeta(name: String, meta: VocabMeta) { 81 | nameToMeta.set(name,meta); 82 | } 83 | 84 | public function getMeta(name: String): VocabMeta { 85 | return nameToMeta.get(name); 86 | } 87 | 88 | public function reverse(id: Int) : String { 89 | return codeToName.get(id); 90 | } 91 | 92 | public function getNames(decorated: Bool = false) : Array { 93 | if (!decorated) { 94 | return [for (i in nameToCode.keys()) i]; 95 | } 96 | var result: Array = []; 97 | for (i in nameToCode.keys()) { 98 | if (used.get(i) != null) { 99 | result.push(i + " = " +nameToCode.get(i)); 100 | } else { 101 | result.push("((" + i + " = " +nameToCode.get(i) + "))"); 102 | } 103 | } 104 | return result; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/cosmicos/Vocab.ts: -------------------------------------------------------------------------------- 1 | export interface Vocab { 2 | get(name: string): number; 3 | reverse(id: number): string; 4 | use(name: string): void; 5 | } 6 | 7 | export class WideVocab implements Vocab { 8 | private nameToCode = new Map(); 9 | private codeToName = new Map(); 10 | private used = new Set(); 11 | private nextId: number = 1; 12 | 13 | public get(name: string): number { 14 | if (!this.nameToCode.has(name)) { 15 | while (this.codeToName.has(this.nextId)) { 16 | this.nextId++; 17 | } 18 | this.nameToCode.set(name, this.nextId); 19 | this.codeToName.set(this.nextId, name); 20 | this.nextId++; 21 | } 22 | return this.nameToCode.get(name)!; 23 | } 24 | 25 | public use(name: string): void { 26 | this.used.add(name); 27 | } 28 | 29 | public reverse(id: number): string { 30 | return this.codeToName.get(id) || `_${id}`; 31 | } 32 | 33 | public set(name: string, id: number): number { 34 | this.nameToCode.set(name, id); 35 | this.codeToName.set(id, name); 36 | return id; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/cosmicos/VocabMeta.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | package cosmicos; 4 | 5 | /* 6 | * Extra information about a piece of vocabulary for human help in the console. 7 | */ 8 | @:expose 9 | class VocabMeta { 10 | public var description: String; 11 | public var example: String; 12 | 13 | public function new(description: String, example: String) { 14 | this.description = description; 15 | this.example = example; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/filter/drawgate-ppm.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | use strict; 4 | 5 | my $s = 6; 6 | my $s2 = 8; 7 | 8 | my %arr; 9 | 10 | while (<>) { 11 | if ($_ =~ /([\-0-9]+) ([\-0-9]+) ([\-0-9]+) ([\-0-9]+)( ([\-0-9]+))?/) { 12 | my $xmid = $1; 13 | my $ymid = $2; 14 | my $dx = $4; 15 | my $dy = $3; 16 | my $v = $6 || 0; 17 | if ($v) { 18 | #$p->setcolour(255,0,0); 19 | } 20 | for (my $i=-$s+1; $i<$s; $i++) { 21 | my $x = ($xmid*$s2+$dy*$i); 22 | my $y = ($ymid*$s2+$dx*$i); 23 | $arr{"$x $y"} = 1; 24 | } 25 | if ($v) { 26 | for (my $i=-$s+1; $i<$s-1; $i++) { 27 | my $x = ($xmid*$s2+$dy*$i+$dx); 28 | my $y = ($ymid*$s2+$dx*$i-$dy); 29 | $arr{"$x $y"} = 1; 30 | my $x2 = ($xmid*$s2+$dy*$i-$dx); 31 | my $y2 = ($ymid*$s2+$dx*$i+$dy); 32 | $arr{"$x2 $y2"} = 1; 33 | } 34 | } 35 | for (my $i=0; $i<3; $i++) { 36 | for (my $j=-$i; $j<=$i; $j++) { 37 | my $x = ($xmid*$s2+$dy*($s-$i)-$dx*$j); 38 | my $y = ($ymid*$s2+$dx*($s-$i)+$dy*$j); 39 | $arr{"$x $y"} = 1; 40 | } 41 | #my $x2 = ($xmid*$s2+$dy*($s-$i)+$dx*$i); 42 | #my $y2 = ($ymid*$s2+$dx*($s-$i)-$dy*$i); 43 | #$arr{"$x2 $y2"} = 1; 44 | } 45 | my $xon = ($dx)*0.2; 46 | my $yon = ($dy)*0.2; 47 | my $xoff = $yon; 48 | my $yoff = $xon; 49 | if ($v) { 50 | #$p->setcolour(0,0,0); 51 | } 52 | } 53 | } 54 | 55 | 56 | my $xmin = 100000; 57 | my $ymin = 100000; 58 | my $xmax = 0; 59 | my $ymax = 0; 60 | 61 | foreach my $key (keys %arr) { 62 | if ($key =~ /([0-9]+) ([0-9]+)/) { 63 | my $x = $1; 64 | my $y = $2; 65 | if ($x>$xmax) { $xmax = $x; } 66 | if ($y>$ymax) { $ymax = $y; } 67 | if ($x<$xmin) { $xmin = $x; } 68 | if ($y<$ymin) { $ymin = $y; } 69 | } 70 | } 71 | 72 | my $d1 = $ymax+$ymin+8; 73 | my $d2 = $xmax+$xmin+8; 74 | 75 | for (my $y=0; $y<$d1; $y++) { 76 | $arr{"0 $y"} = 1; 77 | $arr{($d2-1) . " $y"} = 1; 78 | } 79 | for (my $x=0; $x<$d2; $x++) { 80 | $arr{"$x 0"} = 1; 81 | $arr{"$x " . ($d1-1)} = 1; 82 | } 83 | 84 | print "P3\n"; 85 | print "# view.ppm\n"; 86 | print "$d2 $d1\n"; 87 | print "255\n"; 88 | 89 | for (my $y=0; $y<$d1; $y++) { 90 | for (my $x=0; $x<$d2; $x++) { 91 | my $v = 0; 92 | if (defined($arr{"$x $y"})) { 93 | $v = 1; 94 | } 95 | if ($x>0) { print " "; } 96 | my $txt = "255 0 0"; 97 | if (!$v) { 98 | $txt = "255 255 255"; 99 | } 100 | print $txt; 101 | } 102 | print "\n"; 103 | } 104 | 105 | -------------------------------------------------------------------------------- /src/filter/drawgate-txt.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | use strict; 4 | 5 | my $s = 6; 6 | my $s2 = 8; 7 | 8 | my %arr; 9 | 10 | my @line = (); 11 | 12 | while (<>) { 13 | if ($_ =~ /^([\-0-9]+) ([\-0-9]+) ([\-0-9]+) ([\-0-9]+)( ([\-0-9]+))?/) { 14 | my $xmid = $1; 15 | my $ymid = $2; 16 | my $dx = $4; 17 | my $dy = $3; 18 | my $v = $6 || 0; 19 | chomp($_); 20 | push(@line,$_); 21 | if ($v) { 22 | #$p->setcolour(255,0,0); 23 | } 24 | for (my $i=-$s+1; $i<$s; $i++) { 25 | my $x = ($xmid*$s2+$dy*$i); 26 | my $y = ($ymid*$s2+$dx*$i); 27 | $arr{"$x $y"} = 1; 28 | } 29 | if ($v) { 30 | for (my $i=-$s+1; $i<$s-1; $i++) { 31 | my $x = ($xmid*$s2+$dy*$i+$dx); 32 | my $y = ($ymid*$s2+$dx*$i-$dy); 33 | $arr{"$x $y"} = 1; 34 | my $x2 = ($xmid*$s2+$dy*$i-$dx); 35 | my $y2 = ($ymid*$s2+$dx*$i+$dy); 36 | $arr{"$x2 $y2"} = 1; 37 | } 38 | } 39 | for (my $i=0; $i<3; $i++) { 40 | for (my $j=-$i; $j<=$i; $j++) { 41 | my $x = ($xmid*$s2+$dy*($s-$i)-$dx*$j); 42 | my $y = ($ymid*$s2+$dx*($s-$i)+$dy*$j); 43 | $arr{"$x $y"} = 1; 44 | } 45 | #my $x2 = ($xmid*$s2+$dy*($s-$i)+$dx*$i); 46 | #my $y2 = ($ymid*$s2+$dx*($s-$i)-$dy*$i); 47 | #$arr{"$x2 $y2"} = 1; 48 | } 49 | my $xon = ($dx)*0.2; 50 | my $yon = ($dy)*0.2; 51 | my $xoff = $yon; 52 | my $yoff = $xon; 53 | if ($v) { 54 | #$p->setcolour(0,0,0); 55 | } 56 | } 57 | } 58 | 59 | 60 | my $xmin = 100000; 61 | my $ymin = 100000; 62 | my $xmax = 0; 63 | my $ymax = 0; 64 | 65 | foreach my $key (keys %arr) { 66 | if ($key =~ /([0-9]+) ([0-9]+)/) { 67 | my $x = $1; 68 | my $y = $2; 69 | if ($x>$xmax) { $xmax = $x; } 70 | if ($y>$ymax) { $ymax = $y; } 71 | if ($x<$xmin) { $xmin = $x; } 72 | if ($y<$ymin) { $ymin = $y; } 73 | } 74 | } 75 | 76 | my $d1 = $ymax+$ymin+8; 77 | my $d2 = $xmax+$xmin+8; 78 | 79 | for (my $y=0; $y<$d1; $y++) { 80 | $arr{"0 $y"} = 1; 81 | $arr{($d2-1) . " $y"} = 1; 82 | } 83 | for (my $x=0; $x<$d2; $x++) { 84 | $arr{"$x 0"} = 1; 85 | $arr{"$x " . ($d1-1)} = 1; 86 | } 87 | 88 | print "# GATE testing alternate primer based on gates: CIRCUIT_NAME circuit\n"; 89 | print "# This section contains one or more representations of a circuit\n"; 90 | print "# constructed using UNLESS gates.\n"; 91 | 92 | print "(intro CIRCUIT_NAME_gate);\n"; 93 | print "(intro CIRCUIT_NAME_image);\n"; 94 | print "(define CIRCUIT_NAME_gate | vector "; 95 | 96 | @line = sort { 97 | (my $a1, my $a2, my $a3, my $a4, my @rema) = split(/ /,$a); 98 | (my $b1, my $b2, my $b3, my $b4, my @remb) = split(/ /,$b); 99 | # $a1 -= $a3; $a2 -= $a4; 100 | # $b1 -= $b3; $b2 -= $b4; 101 | my $v = ($a1 <=> $b1); 102 | if ($v!=0) { return $v; } 103 | return ($a2 <=> $b2); 104 | } @line; 105 | 106 | foreach my $l (@line) { 107 | if ($l =~ /^([\-0-9]+) ([\-0-9]+) ([\-0-9]+) ([\-0-9]+)( ([\-0-9]+))?/) { 108 | my $xmid = $1; 109 | my $ymid = $2; 110 | my $dx = $3; 111 | my $dy = $4; 112 | my $v = $6 || 0; 113 | my $x1 = $xmid-$dx; 114 | my $y1 = $ymid-$dy; 115 | my $x2 = $xmid+$dx; 116 | my $y2 = $ymid+$dy; 117 | my $v2 = $v?"\$true":"\$false"; 118 | print "\n (vector $x1 $y1 $x2 $y2 $v2)"; 119 | } 120 | } 121 | print ");\n"; 122 | 123 | print "(define CIRCUIT_NAME_image | make-image $d1 $d2 | vector "; 124 | for (my $y=0; $y<$d1; $y++) { 125 | print "\n ("; 126 | for (my $x=0; $x<$d2; $x++) { 127 | my $v = 0; 128 | if (defined($arr{"$x $y"})) { 129 | $v = 1; 130 | } 131 | # if ($x>0) { print " "; } 132 | my $txt = ":"; 133 | if (!$v) { 134 | $txt = "."; 135 | } 136 | print $txt; 137 | } 138 | print ")"; 139 | } 140 | print ");\n"; 141 | 142 | 143 | print "(equal \$CIRCUIT_NAME_gate | distill-circuit \$CIRCUIT_NAME_image);\n"; 144 | 145 | #print "(distill-circuit (CIRCUIT_NAME_image));\n"; 146 | -------------------------------------------------------------------------------- /src/filter/java-comment.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | use strict; 4 | 5 | use Tie::File; 6 | 7 | my $java = $ARGV[1]; 8 | $java =~ s/\.class/\.java/; 9 | tie my @target, 'Tie::File', $ARGV[0]; 10 | tie my @src, 'Tie::File', $java; 11 | 12 | foreach my $t (@target) { 13 | if ($t =~ /\# CODE/) { 14 | foreach my $s (@src) { 15 | print "# $s\n"; 16 | } 17 | } else { 18 | print $t, "\n"; 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/filter/wrap.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | use strict; 4 | 5 | my $msg = ""; 6 | while (<>) { 7 | $msg = $msg . $_; 8 | } 9 | 10 | $msg =~ s/\n//g; 11 | $msg =~ s/([a-z0-9]{80})/$1\n/g; 12 | print "$msg\n"; 13 | -------------------------------------------------------------------------------- /src/font/SpiderScrawl.d.ts: -------------------------------------------------------------------------------- 1 | export interface Zing { 2 | x: number; 3 | } 4 | 5 | export const cosmicos: any; 6 | -------------------------------------------------------------------------------- /src/font/fontcustom.yml: -------------------------------------------------------------------------------- 1 | # --------------------------------------------------------------------------- # 2 | # Project Info 3 | # Defaults shown. Learn more about these options by running 4 | # `fontcustom help` or visiting . 5 | # --------------------------------------------------------------------------- # 6 | 7 | font_name: fontcustom 8 | css_selector: .icon-{{glyph}} 9 | preprocessor_path: "" 10 | autowidth: false 11 | no_hash: false 12 | force: false 13 | debug: true 14 | quiet: false 15 | 16 | 17 | # --------------------------------------------------------------------------- # 18 | # Input Paths 19 | # --------------------------------------------------------------------------- # 20 | 21 | input: 22 | vectors: ../vectors 23 | 24 | 25 | # --------------------------------------------------------------------------- # 26 | # Output Paths 27 | # --------------------------------------------------------------------------- # 28 | 29 | output: 30 | fonts: app/assets/fonts # required 31 | css: app/assets/stylesheets 32 | preview: app/views/styleguide 33 | 34 | 35 | # --------------------------------------------------------------------------- # 36 | # Templates 37 | # Included in Font Custom: preview, css, scss, scss-rails 38 | # Custom templates should be saved in the INPUT[:templates] directory and 39 | # referenced by their baserame. 40 | # --------------------------------------------------------------------------- # 41 | 42 | templates: 43 | - scss 44 | - preview 45 | -------------------------------------------------------------------------------- /src/font/glyphs_to_svg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | echo "Working in $1" 6 | sleep 2 7 | cd "$1" 8 | 9 | rm -rf *.ff *.ttf *.pgm out/ fontcustom/ .fontcustom* 10 | 11 | echo "New()" > ff.ff 12 | echo "Reencode(\"unicode\")" >> ff.ff 13 | 14 | function add_group { 15 | name="$1" 16 | offset="$2" 17 | let cursor=61696+$offset # this is 0xf100 + offset 18 | for f in `ls -1 *_$name_*.png | sort`; do 19 | b=`basename $f .png` 20 | echo "Working on $f" 21 | ls -1 $f 22 | identify $f 23 | convert $f text.pgm && potrace --svg --flat text.pgm 24 | mv text.svg $b.svg 25 | # fontforge chokes on completely empty path 26 | # and mac fusses about sed -i 27 | sed 's|||' $b.svg > $b.2.svg 28 | rm $b.svg 29 | mv $b.2.svg $b.svg 30 | rm -f text.pgm 31 | echo "Select($cursor);" >> ff.ff 32 | echo "Import(\"$b.svg\");" >> ff.ff 33 | let cursor=$cursor+1 34 | done 35 | } 36 | 37 | add_group "spider" 0 38 | add_group "octo" 512 39 | 40 | echo "Generate(\"test.ttf\")" >> ff.ff 41 | 42 | # after all that, let's just leave it for fontcustom to generate the font 43 | # fontforge -script ff.ff 44 | 45 | echo "autowidth: true" > fontcustom.yml 46 | 47 | # actually, now we use fontcustom 48 | fontcustom compile --no-hash -o out -n cosmic_spider 49 | 50 | cp out/* $2 51 | -------------------------------------------------------------------------------- /src/font/msg2glyphs.js: -------------------------------------------------------------------------------- 1 | var cos = require('SpiderScrawl').cosmicos; 2 | var ss = new cos.SpiderScrawl(null,0,0); 3 | var fs = require('fs'); 4 | fs.readFile(process.argv[2], function (err, data) { 5 | if (err) throw err; 6 | var txt = ss.addString("" + data); 7 | console.log(txt); 8 | }); 9 | -------------------------------------------------------------------------------- /src/font/msg2ogham.js: -------------------------------------------------------------------------------- 1 | var cos = require('OghamStyle').cosmicos; 2 | var ss = new cos.OghamStyle(); 3 | var fs = require('fs'); 4 | fs.readFile(process.argv[2], function (err, data) { 5 | if (err) throw err; 6 | var txt = ss.addString("" + data); 7 | console.log(txt); 8 | }); 9 | -------------------------------------------------------------------------------- /src/gate/UnlessDraw.ts: -------------------------------------------------------------------------------- 1 | import {Canvas} from 'canvas'; 2 | import {UnlessGrid} from './UnlessGrid'; 3 | 4 | export interface DrawOptions { 5 | scale: number; 6 | xOffset?: number; 7 | yOffset?: number; 8 | showLabels?: boolean; 9 | } 10 | 11 | export function draw(cvs: Canvas, grid: UnlessGrid, options: DrawOptions) { 12 | const sc = options.scale; 13 | const ix = options.xOffset || 0; 14 | const iy = options.yOffset || 0; 15 | const showLabels = options.showLabels === undefined ? true : options.showLabels; 16 | const offx = 1; 17 | const offy = 1; 18 | const ctx = cvs.getContext("2d"); 19 | ctx.fillStyle = "#fafafa"; 20 | ctx.fillRect(0,0,cvs.width,cvs.height); 21 | for (let i=0; i { 11 | const out = fse.createWriteStream(fname); 12 | const stream = canvas.createPNGStream(); 13 | stream.on('data', function(chunk){ 14 | out.write(chunk); 15 | }); 16 | stream.on('end', function(){ 17 | out.end(() => { 18 | console.log('saved png ' + fname); 19 | resolve(); 20 | }); 21 | }); 22 | }); 23 | } 24 | 25 | function applyStep(grid: UnlessGrid, step: number|{[key: string]: boolean}): number { 26 | if (typeof step === 'number') { 27 | return step; 28 | } 29 | for (const key of Object.keys(step)) { 30 | const val = step[key]; 31 | grid.force(key, val); 32 | } 33 | return 0; 34 | } 35 | 36 | async function makeImages(name: string, dir: string) { 37 | const sc = 15; 38 | const network = getGridExample(name); 39 | const grid = new UnlessGrid(network.code); 40 | const [x0, y0, x1, y1] = grid.getBounds(); 41 | console.log(x0, y0, x1, y1); 42 | const w = (x1 - x0 + 2) * sc + 2 * sc; 43 | const h = (y1 - y0 + 2) * sc + 2 * sc; 44 | const dx = - x0 * sc + sc; 45 | const dy = - y0 * sc + sc; 46 | var canvas = new Canvas(w, h); 47 | const options: DrawOptions = { 48 | scale: sc, 49 | xOffset: dx, 50 | yOffset: dy, 51 | showLabels: false, 52 | }; 53 | 54 | let at: number = 0; 55 | const fnames: string[] = []; 56 | let active: boolean = false; 57 | for (const act of network.sequence) { 58 | const steps = applyStep(grid, act); 59 | if (steps === -1) { active = true; } 60 | for (let i = 0; i < steps; i++) { 61 | if (active) { 62 | draw(canvas, grid, options); 63 | const fname = `${dir}/${100000 + at}.png`; 64 | await saveImage(canvas, fname); 65 | fnames.push(fname); 66 | at++; 67 | } 68 | grid.update(); 69 | } 70 | } 71 | return fnames; 72 | } 73 | 74 | async function main(name: string) { 75 | name = name || 'd'; 76 | const dir = '/tmp/zig'; 77 | await fse.remove(dir); 78 | await fse.mkdirp(dir); 79 | const fnames = await makeImages(name, dir); 80 | const args = ['-delay', '10', ...fnames, `${name}.gif`]; 81 | console.log("convert", args); 82 | child_process.execFileSync('convert', args); 83 | console.log("made", `${name}.gif`); 84 | } 85 | 86 | main(process.argv[2]).catch(err => { 87 | console.log("Failure:", err); 88 | }); 89 | -------------------------------------------------------------------------------- /src/java/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | find_package(Java REQUIRED) 3 | include(UseJava) 4 | 5 | find_jar(BCEL_JAR NAMES bcel) 6 | if(NOT BCEL_JAR) 7 | message(FATAL_ERROR "could not find bcel.jar, please install libbcel-java or similar") 8 | endif() 9 | set(CMAKE_JAVA_INCLUDE_PATH ${BCEL_JAR} ${CMAKE_JAVA_INCLUDE_PATH}) 10 | 11 | add_jar(Fritzifier ${CMAKE_CURRENT_SOURCE_DIR}/Fritzifier.java ENTRY_POINT Fritzifier) 12 | set_target_properties(Fritzifier PROPERTIES EXCLUDE_FROM_ALL 1) 13 | 14 | add_jar(UnlessDriver ${CMAKE_CURRENT_SOURCE_DIR}/UnlessDriver.java ENTRY_POINT UnlessDriver) 15 | set_target_properties(UnlessDriver PROPERTIES EXCLUDE_FROM_ALL 1) 16 | 17 | -------------------------------------------------------------------------------- /src/java/GridLoader.java: -------------------------------------------------------------------------------- 1 | 2 | //package unless; 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | public class GridLoader { 8 | public static UnlessGrid loadGrid(String name) { 9 | UnlessGrid grid = new UnlessGrid(); 10 | try { 11 | //System.out.println("Reading from file " + name); 12 | BufferedReader reader = new BufferedReader(new FileReader(name)); 13 | boolean done = false; 14 | while (!done) { 15 | String line = reader.readLine(); 16 | //System.out.println(">>> " + line); 17 | if (line==null) { 18 | done = true; 19 | } else { 20 | 21 | //String[] parts = line.split(" "); 22 | //for 1.4 compat, need to be a bit more roundabout 23 | List partsList = new ArrayList(); 24 | StringBuffer tmp = new StringBuffer(""); 25 | for (int i=0; i=4) { 57 | String label = null; 58 | if (partsList.size()>=5) { 59 | //System.out.println(partsList); 60 | label = (String)partsList.get(4); 61 | } 62 | //System.out.println("add " + lst + "[" + label + "]"); 63 | grid.add(((Integer)lst.get(0)).intValue(), 64 | ((Integer)lst.get(1)).intValue(), 65 | ((Integer)lst.get(3)).intValue(), 66 | ((Integer)lst.get(2)).intValue(), 67 | label); 68 | } 69 | } 70 | } 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | throw (new RuntimeException("uh-oh")); 74 | } 75 | return grid; 76 | } 77 | 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/java/Unless.java: -------------------------------------------------------------------------------- 1 | 2 | //package unless; 3 | 4 | public class Unless { 5 | private boolean state = true; 6 | private boolean nextState = true; 7 | private boolean forced = false; 8 | private Unless src = null; 9 | private Unless dest = null; 10 | 11 | public void setSource(Unless src) { 12 | this.src = src; 13 | } 14 | 15 | public void setDestination(Unless dest) { 16 | this.dest = dest; 17 | } 18 | 19 | public boolean getState() { 20 | return state; 21 | } 22 | 23 | public void block() { 24 | nextState = false; 25 | } 26 | 27 | public void prepareForUpdate() { 28 | if (!forced) { 29 | nextState = true; 30 | if (src!=null) { 31 | nextState = src.getState(); 32 | } 33 | } 34 | } 35 | 36 | public void update() { 37 | if (!forced) { 38 | if (state) { 39 | if (dest!=null) { 40 | dest.block(); 41 | } 42 | } 43 | } 44 | } 45 | 46 | public void finalizeUpdate() { 47 | if (!forced) { 48 | state = nextState; 49 | } 50 | forced = false; 51 | } 52 | 53 | public Unless getSource() { 54 | return src; 55 | } 56 | 57 | public Unless getDestination() { 58 | return dest; 59 | } 60 | 61 | public void set(boolean state) { 62 | this.state = state; 63 | forced = true; 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/java/UnlessDriver.java: -------------------------------------------------------------------------------- 1 | 2 | //package unless; 3 | 4 | public class UnlessDriver { 5 | 6 | public static void exercise() { 7 | UnlessNet net = new UnlessNet(); 8 | net.add("john"); 9 | net.add("mary"); 10 | net.add("liam"); 11 | net.add("paul"); 12 | net.add("deirdre"); 13 | net.setDestination("john","mary"); 14 | net.setDestination("mary","liam"); 15 | System.out.println(net); 16 | net.update(); 17 | System.out.println(net); 18 | net.update(); 19 | System.out.println(net); 20 | net.update(); 21 | System.out.println(net); 22 | 23 | System.out.println("*************************************"); 24 | System.out.println("*************************************"); 25 | System.out.println("*************************************"); 26 | 27 | UnlessGrid grid = new UnlessGrid(); 28 | grid.add(3,6,1,0,""); 29 | grid.add(3,10,1,0,""); 30 | grid.add(5,6,1,0,""); 31 | UnlessNet net2 = grid.compile(); 32 | System.out.println(net2); 33 | 34 | 35 | UnlessGrid grid3 = GridLoader.loadGrid("src/D.txt"); 36 | UnlessNet net3 = grid3.compile(); 37 | 38 | System.out.println(net3); 39 | 40 | for (int i=0; i<120; i++) { 41 | String str = grid3.render(net3); 42 | if (i>=20&&i<=60) { 43 | net3.get(grid3.getLabel("DATA")).set(false); 44 | } 45 | if (i>=40&&i<=80) { 46 | net3.get(grid3.getLabel("CLK")).set(false); 47 | } 48 | System.out.println("*************************************"); 49 | //System.out.println(">>> clock_" + String.format("%03d",i)); 50 | System.out.print(str); 51 | net3.update(); 52 | } 53 | } 54 | 55 | public static void showGrid(String name) { 56 | UnlessGrid grid = GridLoader.loadGrid(name); 57 | UnlessNet net = grid.compile(); 58 | for (int i=0; i<100; i++) { 59 | net.update(); 60 | } 61 | System.out.println(">>> " + name); 62 | System.out.println(grid.render(net)); 63 | } 64 | 65 | public static void main(String[] arg) { 66 | for (int i=0; i/dev/null || { 6 | echo "CMake not found. Please install." 7 | exit 1 8 | } 9 | 10 | hash make 2>/dev/null || { 11 | echo "make not found. Please install." 12 | exit 1 13 | } 14 | 15 | arg="$1" 16 | message=${arg-:} 17 | mkdir -p build 18 | if [[ -e "variant/$message.cmake" ]]; then 19 | shift 20 | echo "$message" > build/last_message.txt 21 | else 22 | message="" 23 | fi 24 | 25 | if [[ "$message" = "" ]]; then 26 | if [[ -e build/last_message.txt ]]; then 27 | message=`cat build/last_message.txt` 28 | fi 29 | fi 30 | 31 | if [[ "$message" = "" ]]; then 32 | echo "Do not know which message to build." 33 | echo "Try any of:" 34 | echo " make tiny" 35 | echo " make standard" 36 | exit 1 37 | fi 38 | 39 | echo "Working on message: $message" 40 | 41 | mkdir -p build/$message 42 | cd build/$message 43 | if [ "$1" = "configure" ]; then 44 | ccmake ../.. 45 | else 46 | cmake -DCOSMIC_VARIANT=$message ../.. 47 | make "$@" && echo "Result in build/$message" 48 | fi 49 | -------------------------------------------------------------------------------- /src/prepare_node.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | if [ ! -e node_modules_docker ]; then 6 | if [ -e node_modules ]; then 7 | mv node_modules node_modules_host 8 | fi 9 | echo "First time operation: installing node packages" 10 | npm install 11 | mv node_modules node_modules_docker 12 | if [ -e node_modules_host ]; then 13 | mv node_modules_host node_modules 14 | fi 15 | fi 16 | -------------------------------------------------------------------------------- /src/tests/test_audio.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // First, run "haxe compile_js.hxml" to get cosmicos.js 4 | // Then run this with NODE_PATH set to make cosmicos.js visible 5 | // On Debian/Ubuntu: NODE_PATH=$PWD nodejs ./test.js 6 | 7 | var cos = require("CosmicAudio").cosmicos; 8 | var snd = new cos.Sound(); 9 | var txt = snd.textToWav("01234543210",false); 10 | 11 | var fs = require('fs'); 12 | fs.writeFileSync("node.wav",txt,"binary"); 13 | console.log("Wrote to node.wav"); 14 | fs.writeFileSync("node.txt",snd.textToWavUrl("01234543210"),"binary"); 15 | console.log("Wrote to node.txt"); 16 | 17 | -------------------------------------------------------------------------------- /src/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | const variant = process.env.COSMIC_VARIANT; 4 | 5 | if (!variant) { 6 | throw new Error('Need COSMIC_VARIANT'); 7 | } 8 | 9 | const common = { 10 | mode: 'development', 11 | resolve: { 12 | modules: [`./build/${variant}/js`, `./build/${variant}`] 13 | } 14 | } 15 | 16 | const cosh = { 17 | ...common, 18 | entry: `./build/${variant}/js/src/cmd/cosh.js`, 19 | target: 'node', 20 | output: { 21 | path: path.resolve('build', variant, 'bin'), 22 | filename: 'cosh.js' 23 | } 24 | }; 25 | 26 | const libCosmicos = { 27 | ...common, 28 | entry: `./build/${variant}/js/src/cmd/Eval.js`, 29 | target: 'web', 30 | output: { 31 | path: path.resolve('build', variant, 'bin'), 32 | filename: 'lib_cosmicos.js', 33 | library: 'Eval', 34 | libraryExport: 'default' 35 | } 36 | }; 37 | 38 | module.exports = [ cosh, libCosmicos ]; 39 | 40 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(WORK ${CMAKE_CURRENT_BINARY_DIR}) 2 | set(SRC1 ${CMAKE_SOURCE_DIR}/src) 3 | set(SRC2 ${CMAKE_SOURCE_DIR}/tests) 4 | 5 | add_custom_command(OUTPUT ${WORK}/test.js 6 | COMMAND ${HAXE} -js test.js -main Main -cp ${SRC1} -cp ${SRC2} 7 | WORKING_DIRECTORY ${WORK} 8 | DEPENDS 9 | ${SRC2}/EvaluateTest.hx 10 | ${SRC2}/FourSymbolTest.hx 11 | ${SRC2}/Main.hx 12 | ${SRC2}/NormalizeTest.hx 13 | ${SRC2}/ParseTest.hx 14 | ${SRC2}/TranslateTest.hx 15 | ${SRC2}/UnflattenTest.hx 16 | ${CMAKE_BINARY_DIR}/src/CosmicEval.js) 17 | 18 | foreach(test ParseTest NormalizeTest UnflattenTest TranslateTest FourSymbolTest EvaluateTest) 19 | add_test(NAME ${test} COMMAND ${NODE} ${WORK}/test.js ${test}) 20 | endforeach() 21 | 22 | add_custom_target(harness ALL DEPENDS 23 | ${WORK}/test.js 24 | jshelpers 25 | ) 26 | -------------------------------------------------------------------------------- /tests/EvaluateTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class EvaluateTest extends haxe.unit.TestCase { 4 | 5 | public function testBasic() { 6 | var state = new cosmicos.State(); 7 | var vocab = state.getVocab(); 8 | var mem = state.getMemory(); 9 | var codec = new cosmicos.ChainCodec([ 10 | new cosmicos.ParseCodec(vocab), 11 | new cosmicos.NormalizeCodec(vocab), 12 | new cosmicos.UnflattenCodec(), 13 | new cosmicos.TranslateCodec(state), 14 | new cosmicos.EvaluateCodec(state) 15 | ]); 16 | var statement = new cosmicos.Statement("+ 1 | * 2 3;"); 17 | codec.encode(statement); 18 | assertEquals(7, statement.content[0]); 19 | } 20 | 21 | public function testEvaluateAndEncode() { 22 | var state = new cosmicos.State(); 23 | state.useIntVocab(); 24 | var vocab = state.getVocab(); 25 | var normalize = new cosmicos.ChainCodec([ 26 | new cosmicos.ParseCodec(vocab), 27 | new cosmicos.NormalizeCodec(vocab) 28 | ]); 29 | var coding = new cosmicos.FourSymbolCodec(vocab); 30 | var evaluate = new cosmicos.ChainCodec([ 31 | new cosmicos.UnflattenCodec(), 32 | new cosmicos.TranslateCodec(state), 33 | new cosmicos.EvaluateCodec(state) 34 | ]); 35 | 36 | var statement = new cosmicos.Statement("+ 1 | * 2 3;"); 37 | normalize.encode(statement); 38 | coding.encode(statement); 39 | 40 | var coded = statement.copy().content[0]; 41 | 42 | coding.decode(statement); 43 | evaluate.encode(statement); 44 | var result = statement.content[0]; 45 | 46 | assertEquals(7, result); 47 | for (i in 0...coded.length) { 48 | var ch = coded.charAt(i); 49 | assertTrue(ch >= '0' && ch <= '3'); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/FourSymbolTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class FourSymbolTest extends haxe.unit.TestCase { 4 | 5 | public function testBasic() { 6 | var state = new cosmicos.State(); 7 | state.useIntVocab(); 8 | var eval = new cosmicos.Evaluate(state); 9 | eval.applyOldOrder(); 10 | eval.addStdMin(); 11 | var vocab = eval.getVocab(); 12 | var codec = new cosmicos.ChainCodec([ 13 | new cosmicos.ParseCodec(vocab), 14 | new cosmicos.NormalizeCodec(vocab), 15 | new cosmicos.FourSymbolCodec(vocab), 16 | ]); 17 | var tests: Array> = [ 18 | ["+ 1 | * 2 3;", 7], 19 | ["assign x 2 | + 2 | * (x) 3;", 8], 20 | ["assign x 2 | + 3 | * $x 3;", 9], 21 | ["intro foo;", 1], 22 | ["= (unary 1 0) (unary 1 0)", 1], 23 | ["= 1 1", 1], 24 | ["+ (:::) 1", 8], 25 | ["= 5 | + (:.) (::)", 1], 26 | ["+ 0 | + (::::) (:...:)", 32], 27 | ["< 10 | + (:.........................:) (::::::::::::::::::::::::::::::::)", 1], 28 | ]; 29 | 30 | for (item in tests) { 31 | var input = item[0]; 32 | var output = item[1]; 33 | var statement = new cosmicos.Statement(input); 34 | codec.encode(statement); 35 | codec.decode(statement); 36 | var result = eval.evaluateLine(statement.content[0]); 37 | assertEquals(output, result); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Main.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class Main { 4 | 5 | public function new() { 6 | } 7 | 8 | public function body() : Int { 9 | var r = new haxe.unit.TestRunner(); 10 | var cases = [ 11 | new ParseTest(), 12 | new NormalizeTest(), 13 | new UnflattenTest(), 14 | new TranslateTest(), 15 | new FourSymbolTest(), 16 | new EvaluateTest(), 17 | ]; 18 | 19 | #if js 20 | var args = untyped __js__("process.argv.slice(2)"); 21 | #else 22 | var args = Sys.args; // this might be out by one, not tested 23 | #end 24 | var filter = args[0]; 25 | for (c in cases) { 26 | var name = Type.getClassName(Type.getClass(c)); 27 | if (filter=="" || name.indexOf(filter)>=0) { 28 | r.add(c); 29 | } 30 | } 31 | var ok = r.run(); 32 | if (!ok) { 33 | return 1; 34 | } 35 | return 0; 36 | } 37 | 38 | static public function main() { 39 | var main = new Main(); 40 | var result = main.body(); 41 | #if js 42 | untyped __js__("process.exit(result)"); 43 | #else 44 | Sys.exit(result); 45 | #end 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/NormalizeTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class NormalizeTest extends haxe.unit.TestCase { 4 | public function roundTrip(str: String) : String { 5 | var vocab = new cosmicos.Vocab(); 6 | var parse : cosmicos.Codec = new cosmicos.ParseCodec(vocab); 7 | var norm : cosmicos.Codec = new cosmicos.NormalizeCodec(vocab); 8 | var statement = new cosmicos.Statement(str); 9 | parse.encode(statement); 10 | norm.encode(statement); 11 | norm.decode(statement); 12 | parse.decode(statement); 13 | assertEquals(1, statement.content.length); 14 | return statement.content[0]; 15 | } 16 | 17 | public function assertRoundTrip(str: String) { 18 | var result = roundTrip(str); 19 | assertEquals(str, result); 20 | } 21 | 22 | public function testBasic() { 23 | var statements : Array = [ 24 | "+ :..: 1;", 25 | "demo \"test\" 1;", 26 | "@ foo 1;" 27 | ]; 28 | for (i in 0...statements.length) { 29 | assertRoundTrip(statements[i]); 30 | } 31 | } 32 | 33 | public function testDefine() { 34 | var txt = roundTrip("define foo 1;"); 35 | assertEquals("@ foo 1;", txt); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/ParseTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class ParseTest extends haxe.unit.TestCase { 4 | public function roundTrip(str: String) { 5 | var vocab = new cosmicos.Vocab(); 6 | var codec : cosmicos.Codec = new cosmicos.ParseCodec(vocab); 7 | var statement = new cosmicos.Statement(str); 8 | codec.encode(statement); 9 | codec.decode(statement); 10 | assertEquals(1, statement.content.length); 11 | assertEquals(str, statement.content[0]); 12 | } 13 | 14 | public function testBasic() { 15 | var statements : Array = [ 16 | "+ 1 1;", 17 | "+ 1 (* 2 | + $y $x);", 18 | "+ 1 (* (+ 1 | - 2 1) | + $y $x);", 19 | ]; 20 | for (i in 0...statements.length) { 21 | roundTrip(statements[i]); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/TranslateTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class TranslateTest extends haxe.unit.TestCase { 4 | 5 | public function testBasic() { 6 | var eval = new cosmicos.Evaluate(); 7 | eval.addStdMin(); 8 | eval.applyOldOrder(); 9 | var vocab = eval.getVocab(); 10 | var state = eval.getState(); 11 | var codec = new cosmicos.ChainCodec([ 12 | new cosmicos.ParseCodec(vocab), 13 | new cosmicos.NormalizeCodec(vocab), 14 | new cosmicos.UnflattenCodec(), 15 | new cosmicos.TranslateCodec(state) 16 | ]); 17 | var statement = new cosmicos.Statement("= 32 64;"); 18 | codec.encode(statement); 19 | codec.decode(statement); 20 | assertEquals("= 32 64;", statement.content[0]); 21 | eval.evaluateLine("define base-translate $translate;"); 22 | eval.evaluateLine("define translate | ? x | if (= $x 32) 64 (base-translate $x);"); 23 | statement = new cosmicos.Statement("= 32 64;"); 24 | codec.encode(statement); 25 | codec.decode(statement); 26 | assertEquals("= 64 64;", statement.content[0]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/UnflattenTest.hx: -------------------------------------------------------------------------------- 1 | // -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 2 | 3 | class UnflattenTest extends haxe.unit.TestCase { 4 | public function roundTrip(str: String) : String { 5 | var vocab = new cosmicos.Vocab(); 6 | var codec = new cosmicos.ChainCodec([ 7 | new cosmicos.ParseCodec(vocab), 8 | new cosmicos.NormalizeCodec(vocab), 9 | new cosmicos.UnflattenCodec() 10 | ]); 11 | var statement = new cosmicos.Statement(str); 12 | codec.encode(statement); 13 | codec.decode(statement); 14 | assertEquals(1, statement.content.length); 15 | return statement.content[0]; 16 | } 17 | 18 | public function assertRoundTrip(str: String) { 19 | var result = roundTrip(str); 20 | assertEquals(str, result); 21 | } 22 | 23 | public function testBasic() { 24 | var statements : Array = [ 25 | "+ 1 1;", 26 | "+ (+ 1 1) 1;", 27 | "+ 1 | + 1 1;", 28 | "+ 1 | + $x $y;", 29 | ]; 30 | for (i in 0...statements.length) { 31 | assertRoundTrip(statements[i]); 32 | } 33 | } 34 | 35 | public function testExtraFlat() { 36 | var txt = roundTrip("+ 1 (+ 1 1);"); 37 | assertEquals("+ 1 | + 1 1;", txt); 38 | var txt = roundTrip("+ 1 (+ (x) (y));"); 39 | assertEquals("+ 1 | + $x $y;", txt); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/test_basics.ts: -------------------------------------------------------------------------------- 1 | import {FourSymbolCodecV2} from '../src/cosmicos/FourSymbolCodecV2'; 2 | import {GlyphCode} from '../src/cosmicos/GlyphCode'; 3 | import {Rename} from '../src/cosmicos/Rename'; 4 | import {WideStatement} from '../src/cosmicos/Statement'; 5 | import {WideVocab} from '../src/cosmicos/Vocab'; 6 | 7 | describe('four symbol encoding', function() { 8 | it('roundtrip', function() { 9 | const examples = [ 10 | [1, 14, [2, 33, "hello", "hello:there:12"], 3], 11 | [1, 14, [10, [-1, 3, 5]], [-2, "hello"]], 12 | ["is:int", [-1, "unary", "0", "zig:22:there"]] 13 | ]; 14 | for (const example of examples) { 15 | const statement = new WideStatement(example); 16 | console.log(JSON.stringify(statement)); 17 | const vocab = new WideVocab(); 18 | vocab.set("there", 42); 19 | vocab.set("zig", 0b10110110); 20 | const x = new FourSymbolCodecV2(vocab, true); 21 | x.encode(statement); 22 | console.log(JSON.stringify(statement)); 23 | const gc = new GlyphCode('octo'); 24 | console.log(gc.addString(statement.content[0])); 25 | x.decode(statement); 26 | console.log(JSON.stringify(statement)); 27 | } 28 | }); 29 | 30 | it('renaming', function() { 31 | const rename = new Rename(); 32 | rename.add('hello', 'hi'); 33 | console.log(rename.get('test')); 34 | console.log(rename.get('hello')); 35 | console.log(rename.get('hello:test')); 36 | console.log(rename.rename(['test', 1, 2, ['test', 'hello', '1:hello:2']])); 37 | console.log(rename.renameWithinString('1 2 hello hello:there (3 4 hello there)\n hello;')); 38 | rename.add('make-cell', 'cell:make'); 39 | console.log(rename.renameWithinString('@ demo:make-cell:x | make-cell 14;')); 40 | console.log(rename.unget('hi')); 41 | console.log(rename.unrenameWithinString('hi there')); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "lib": ["esnext"], 5 | "module": "commonjs", 6 | "moduleResolution": "node", 7 | "baseUrl": ".", 8 | "resolveJsonModule": true 9 | }, 10 | "include": [ 11 | "src/gate/*", 12 | "src/cosmicos/*", 13 | "src/font/*", 14 | "src/cmd/*", 15 | "msg/*.json" 16 | ], 17 | "exclude": [ 18 | "node_modules" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /variant/iconic.cmake: -------------------------------------------------------------------------------- 1 | include(${CMAKE_SOURCE_DIR}/msg/PARTIAL.cmake) 2 | set(COSMIC_USE_FLATTENER true) 3 | set(COSMIC_VOCAB ${CMAKE_SOURCE_DIR}/variant/iconic/vocab.json) 4 | -------------------------------------------------------------------------------- /variant/nested.cmake: -------------------------------------------------------------------------------- 1 | # we expect to get a variable COSMIC_DEPENDS that lists all parts 2 | include(${CMAKE_SOURCE_DIR}/msg/README.cmake) 3 | 4 | # Flag controlling whether the "|" symbol for flattening messages can be 5 | # encoded (if true), or (if false) should be expanded to nested parens. 6 | set(COSMIC_USE_FLATTENER false) 7 | -------------------------------------------------------------------------------- /variant/standard.cmake: -------------------------------------------------------------------------------- 1 | # we expect to get a variable COSMIC_DEPENDS that lists all parts 2 | include(${CMAKE_SOURCE_DIR}/msg/README.cmake) 3 | 4 | # Flag controlling whether the "|" symbol for flattening messages can be 5 | # encoded (if true), or (if false) should be expanded to nested parens. 6 | set(COSMIC_USE_FLATTENER true) 7 | -------------------------------------------------------------------------------- /variant/tiny.cmake: -------------------------------------------------------------------------------- 1 | # A tiny message, just as an example. 2 | # Message is built from msg/COS_Intro.* and msg/COS_Compare.*. 3 | 4 | set(COSMIC_DEPENDS 5 | COS_Intro 6 | COS_Compare) 7 | -------------------------------------------------------------------------------- /wiki_images/COS_message_picture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulfitz/cosmicos/67e80da32383bd77ad4427455c9ae982e9c649cf/wiki_images/COS_message_picture.png -------------------------------------------------------------------------------- /wiki_images/COS_zipfs_law.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulfitz/cosmicos/67e80da32383bd77ad4427455c9ae982e9c649cf/wiki_images/COS_zipfs_law.png --------------------------------------------------------------------------------