From 08d33f808db30936b273582bcfaddcbc5a57c112 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 6 May 2025 13:44:10 -0700 Subject: Chapter 23: Methods and Revisiting Types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> --- docs/chapter23/18-grammar.md | 132 ++ docs/chapter23/Makefile | 12 + docs/chapter23/README.md | 459 +++++++ docs/chapter23/d3-graphviz.js | 2014 ++++++++++++++++++++++++++++++ docs/chapter23/d3.v7.min.js | 2 + docs/chapter23/examples/A_helloWorld.smp | 3 + docs/chapter23/examples/BubbleSort.smp | 37 + docs/chapter23/font-awesome-4.7.0.zip | Bin 0 -> 669808 bytes docs/chapter23/graphviz.umd.js | 7 + docs/chapter23/ifg_trim.pdf | Bin 0 -> 155190 bytes docs/chapter23/index.html | 218 ++++ docs/chapter23/lattice.gv | 203 +++ docs/chapter23/lattice.svg | 691 ++++++++++ docs/chapter23/lattice2.gv | 85 ++ docs/chapter23/lattice2.svg | 260 ++++ docs/chapter23/pardoe.docx | Bin 0 -> 22583 bytes docs/chapter23/webtcp.min.js | 65 + 17 files changed, 4188 insertions(+) create mode 100644 docs/chapter23/18-grammar.md create mode 100644 docs/chapter23/Makefile create mode 100644 docs/chapter23/README.md create mode 100644 docs/chapter23/d3-graphviz.js create mode 100644 docs/chapter23/d3.v7.min.js create mode 100644 docs/chapter23/examples/A_helloWorld.smp create mode 100644 docs/chapter23/examples/BubbleSort.smp create mode 100644 docs/chapter23/font-awesome-4.7.0.zip create mode 100644 docs/chapter23/graphviz.umd.js create mode 100644 docs/chapter23/ifg_trim.pdf create mode 100644 docs/chapter23/index.html create mode 100644 docs/chapter23/lattice.gv create mode 100644 docs/chapter23/lattice.svg create mode 100644 docs/chapter23/lattice2.gv create mode 100644 docs/chapter23/lattice2.svg create mode 100644 docs/chapter23/pardoe.docx create mode 100644 docs/chapter23/webtcp.min.js (limited to 'docs') diff --git a/docs/chapter23/18-grammar.md b/docs/chapter23/18-grammar.md new file mode 100644 index 00000000..7b611fbf --- /dev/null +++ b/docs/chapter23/18-grammar.md @@ -0,0 +1,132 @@ +# Grammar for Chapter 16 + +```antlrv4 +grammar SimpleLanguage; + +program : block EOF ; + +block : statement+ ; + + +statement + : '{' block '}' + | returnStatement + | ifStatement + | whileStatement + | breakStatement + | continueStatement + | metaStatement + | structDeclaration + | expressionStatement + ; + + +returnStatement : 'return' expression ';' ; + +ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; + +whileStatement : 'while' '(' expression ')' statement ; + +breakStatement : 'break' ';' ; + +continueStatement : 'continue' ';' ; + +metaStatement : '#showGraph' ';' ; + +structDeclaration : 'struct' IDENTIFIER '{' block '}' ; + +expressionStatement + : type IDENTIFIER ';' + | type IDENTIFIER '=' expression ';' + | IDENTIFIER '=' expression ';' + | expression + ; + +PRIMTYPE + : 'int' + | 'i8' + | 'i16' + | 'i32' + | 'i64' + | 'u8' + | 'u16' + | 'u32' + | 'flt' + | 'f32' + | 'f64' + | 'bool' + ; + +type + : PRIMTYPE + | typeName '?'? + | typeName nestedArray+ + ; + +nestedArray : '?'? '[]'* ; + +typeName : IDENTIFIER ; + + +expression : bitWiseExpression ; + +bitWiseExpression + : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* + ; + +shiftExpression + : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-') unaryExpression + | '!' unaryExpression + | primaryExpression postAssign + ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | '(' expression ')' + | 'true' + | 'false' + | 'null' + | newExpression + | IDENTIFIER + ; + +newExpression + : 'new' IDENTIFIER [ '{' block '}' ] + | 'new' IDENTIFIER '[' expression ']' + ; + +postAssign : postFix [ '=' expression ] | [ '#' ]; + +postFix + : '.' IDENTIFIER postFix + | '[' expression ']' postFix + ; + + +IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; + +INTEGER_LITERAL + : [1-9]DIGIT* + | [0] + ; + +NON_DIGIT: [a-zA-Z_]; +DIGIT: [0-9]; +``` diff --git a/docs/chapter23/Makefile b/docs/chapter23/Makefile new file mode 100644 index 00000000..12659b8f --- /dev/null +++ b/docs/chapter23/Makefile @@ -0,0 +1,12 @@ +GV := $(wildcard *.gv) +SVG := $(patsubst %.gv,%.svg,$(GV)) + +.PHONY: all +all: $(SVG) + +%.svg: %.gv + dot -Tsvg $< > $@ + +.PHONY: clean +clean: + @rm -f $(SVG) diff --git a/docs/chapter23/README.md b/docs/chapter23/README.md new file mode 100644 index 00000000..545a4c4a --- /dev/null +++ b/docs/chapter23/README.md @@ -0,0 +1,459 @@ +# Chapter 23: Methods and Revisiting Types + +In this chapter add *methods*, functions defined in structs which take a +hidden `self` argument and can access the struct fields. Here is an +`indexOf` method in a String-like class: + +```java +// A String-like class, with methods +struct String { + u8[~] buf; // Buffer of read-only characters + + // Return the index of the first character 'c' or -1 + val indexOf = { u8 c -> // Hidden 'self' argument + for( int i=0; i