Skip to content

Commit

Permalink
!285 Seperate liii_stack.tmu from GoldfishLang.tmu
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Mar 5, 2025
1 parent 253de7b commit d7be3b8
Show file tree
Hide file tree
Showing 2 changed files with 338 additions and 312 deletions.
312 changes: 0 additions & 312 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -6732,318 +6732,6 @@

\;
</goldfish-chunk>

<chapter|(liii stack)>

<section|许可证>

<\scm-chunk|goldfish/liii/stack.scm|false|true>
;

; Copyright (C) 2024 The Goldfish Scheme Authors

;

; Licensed under the Apache License, Version 2.0 (the "License");

; you may not use this file except in compliance with the License.

; You may obtain a copy of the License at

;

; http://www.apache.org/licenses/LICENSE-2.0

;

; Unless required by applicable law or agreed to in writing, software

; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

; License for the specific language governing permissions and limitations

; under the License.

;

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|false|true>
;

; Copyright (C) 2024 The Goldfish Scheme Authors

;

; Licensed under the Apache License, Version 2.0 (the "License");

; you may not use this file except in compliance with the License.

; You may obtain a copy of the License at

;

; http://www.apache.org/licenses/LICENSE-2.0

;

; Unless required by applicable law or agreed to in writing, software

; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

; License for the specific language governing permissions and limitations

; under the License.

;

\;
</scm-chunk>

<section|接口>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define-library (liii stack)

(import (liii lang))

(export stack)

(begin

\;
</scm-chunk>

<section|测试>

<\goldfish-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(import (liii stack) (liii check))

\;

(check-set-mode! 'report-failed)

\;
</goldfish-chunk>

<section|实现>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define-case-class stack ((data list?))

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
</scm-chunk>

<subsection|选择器>

<paragraph|stack%length>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%length) (length data))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :length) =\<gtr\> 3)

\;
</scm-chunk>

<paragraph|stack%size>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%size) (length data))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :size) =\<gtr\> 3)

\;
</scm-chunk>

<paragraph|stack%top>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%top)

\ \ (if (null? data)

\ \ \ \ \ \ (error 'out-of-range)

\ \ \ \ \ \ (car data)))

\;
</scm-chunk>

<\goldfish-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2)) :top) =\<gtr\> 1)

(check-catch 'out-of-range ((stack (list )) :top))

\;
</goldfish-chunk>

<subsection|转换器>

<paragraph|stack%to-list>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%to-list) (rich-list data))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :to-list) =\<gtr\> ($ (list 1 2 3)))

\;
</scm-chunk>

<subsection|静态方法>

<paragraph|stack@empty>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (@empty) (stack (list )))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack :empty) :length) =\<gtr\> 0)

\;
</scm-chunk>

<subsection|修改器>

<paragraph|stack%pop>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%pop)

\ \ (if (null? data)

\ \ \ \ \ \ (error 'out-of-range "Cannot pop from an empty stack")

\ \ \ \ \ \ (stack (cdr data))))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2)) :pop) =\<gtr\> (stack (list 2)))

(check ((stack (list 1 2 3)) :pop :pop) =\<gtr\> (stack (list 3)))

(check-catch 'out-of-range ((stack :empty) :pop))

\;
</scm-chunk>

<paragraph|stack%pop!>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%pop!)

\ \ (if (null? data)

\ \ \ \ \ \ (error 'out-of-range)

\ \ \ \ \ \ (stack (set! data (cdr data))))

\ \ (%this))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))

\ \ (check (t :pop!) =\<gtr\> (stack (list 2 3)))

\ \ (check (t :pop! :pop!) =\<gtr\> (stack (list )))

\ \ (check-catch 'out-of-range ((stack :empty) :pop!)))

\;
</scm-chunk>

<paragraph|stack%push>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%push element . es)\

\ \ (let1 r (stack (cons element data))

\ \ \ \ (if (null? es)

\ \ \ \ \ \ \ \ r

\ \ \ \ \ \ \ \ (apply r es))))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))

\ \ (check (t :push 1) =\<gtr\> (stack (list 1 1 2 3)))

\ \ (check (t :push 1 :push 1) =\<gtr\> (stack (list 1 1 1 2 3))))

\;
</scm-chunk>

<paragraph|stack%push!>

<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%push! element)\

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (stack (set! data (cons element data)))\

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (%this))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))

\ \ (check (t :push! 1) =\<gtr\> (stack (list 1 1 2 3)))

\ \ (check (t :push! 1 :push! 1) =\<gtr\> (stack (list 1 1 1 1 2 3)))

\ \ (check (t :pop! :push! 2) =\<gtr\> (stack (list 2 1 1 1 2 3))))

\;
</scm-chunk>

<subsection|结尾>

<\scm-chunk|goldfish/liii/stack.scm|true|false>
) ; end of define-case-class

) ; end of begin

) ; end of define-library

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|false>
(check-report)

\;
</scm-chunk>
</body>

<\initial>
Expand Down
Loading

0 comments on commit d7be3b8

Please sign in to comment.