Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Aug 8, 2024
0 parents commit 85063cf
Show file tree
Hide file tree
Showing 251 changed files with 27,135 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.c linguist-language=Go
Empty file added .nojekyll
Empty file.
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2019 <chaishushan{AT}gmail.com>. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

#
# MnBook: Mini Markdown Book
# https://github.com/wa-lang/mnbook
#

default:
mnbook serve

build:
-rm book
mnbook build
-rm book/.gitignore
-rm -rf book/.git

deploy:
-@make clean
mnbook build
-rm book/.gitignore
-rm -rf book/.git
-rm -rf book/examples

cd book && git init
cd book && git add .
cd book && git commit -m "first commit"
cd book && git branch -M gh-pages
cd book && git remote add origin [email protected]:chai2010/go-ast-book.git
cd book && git push -f origin gh-pages

clean:
-rm -rf book
63 changes: 63 additions & 0 deletions appendix/a-goyacc/calc.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright 2019 <chaishushan{AT}gmail.com>. All rights reserved. */
/* Use of this source code is governed by a Apache */
/* license that can be found in the LICENSE file. */

/* simplest version of calculator */

%{
package main

import (
"fmt"
)

var idValueMap = map[string]int{}
%}

%union {
value int
id string
}

%type <value> exp factor term
%token <value> NUMBER
%token <id> ID

%token ADD SUB MUL DIV ABS
%token LPAREN RPAREN ASSIGN
%token EOL

%%
calclist
: // nothing
| calclist exp EOL {
idValueMap["_"] = $2
fmt.Printf("= %v\n", $2)
}
| calclist ID ASSIGN exp EOL {
idValueMap["_"] = $4
idValueMap[$2] = $4
fmt.Printf("= %v\n", $4)
}
;

exp
: factor { $$ = $1 }
| exp ADD factor { $$ = $1 + $3 }
| exp SUB factor { $$ = $1 - $3 }
;

factor
: term { $$ = $1 }
| factor MUL term { $$ = $1 * $3 }
| factor DIV term { $$ = $1 / $3 }
;

term
: NUMBER { $$ = $1 }
| ID { $$ = idValueMap[$1] }
| ABS term { if $2 >= 0 { $$ = $2 } else { $$ = -$2 } }
| LPAREN exp RPAREN { $$ = $2 }
;

%%
18 changes: 18 additions & 0 deletions appendix/a-goyacc/examples/calculator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 <chaishushan{AT}gmail.com>. All rights reserved.
# Use of this source code is governed by a Apache
# license that can be found in the LICENSE file.

run:
make flex
make goyacc

@go fmt
go run .

flex:
flex --prefix=yy --header-file=calc.lex.h -o calc.lex.c calc.l

goyacc:
goyacc -o calc.y.go -p "calc" calc.y

clean:
31 changes: 31 additions & 0 deletions appendix/a-goyacc/examples/calculator/calc.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2019 <chaishushan{AT}gmail.com>. All rights reserved. */
/* Use of this source code is governed by a Apache */
/* license that can be found in the LICENSE file. */

%option noyywrap

%{
#include "tok.h"
%}

%%

[_a-zA-Z]+ { return ID; }
[0-9]+ { return NUMBER; }

"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }

"(" { return LPAREN; }
")" { return RPAREN; }
"=" { return ASSIGN; }

\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { return ILLEGAL; }

%%

Loading

0 comments on commit 85063cf

Please sign in to comment.