diff --git a/TeXmacs/plugins/fish/doc/fish.en.tmu b/TeXmacs/plugins/fish/doc/fish.en.tmu new file mode 100644 index 0000000000..b079897054 --- /dev/null +++ b/TeXmacs/plugins/fish/doc/fish.en.tmu @@ -0,0 +1,84 @@ +> + +> + +<\body> + <\hide-preamble> + + + + + + Code Plugin> + + style package> + + Click > or click |Code|> on the focus toolbar to add the style package. + + In the style package, > is provided for inline snippets and -code> is provided for code blocks. + + syntax> + + The syntax of the language is defined in: + + /progs/code/-lang.scm> + + Here is an example code block with syntax highlight: + + <\fish-code> + ; This is a note + + fish define example + + \ \ \ \ global width = 10.0 + + \ \ \ \ global height = 5.0 + + \ \ \ \ global density = 2000.0 + + \ \ \ \ + + \ \ \ \ local area = width * height + + \ \ \ \ local volume = area * 1.0 + + \ \ \ \ local weight = density * volume * 9.81 + + \ \ \ \ + + \ \ \ \ io.out('width:' + string(width) + ' m') + + \ \ \ \ io.out('height:' + string(height) + ' m') + + \ \ \ \ io.out('area:' + string(area) + ' m^2') + + \ \ \ \ io.out('density:' + string(density) + ' kg/m^3') + + \ \ \ \ io.out('weight:' + string(weight) + ' N') + + end + + \; + + @example + + + editing> + + The editing of the code is defined in: + + /progs/code/-edit.scm> + + \; + + + + + + +<\initial> + <\collection> + + + + diff --git a/TeXmacs/plugins/fish/packages/code/fish.ts b/TeXmacs/plugins/fish/packages/code/fish.ts new file mode 100644 index 0000000000..0755852e93 --- /dev/null +++ b/TeXmacs/plugins/fish/packages/code/fish.ts @@ -0,0 +1,32 @@ + + +> + +<\body> + + + + <\src-purpose> + fish Language + + > + + + + + >>> + + + <\pseudo-code> + > + + > + + +<\initial> + <\collection> + + + + + diff --git a/TeXmacs/plugins/fish/progs/code/fish-edit.scm b/TeXmacs/plugins/fish/progs/code/fish-edit.scm new file mode 100644 index 0000000000..9242d03d55 --- /dev/null +++ b/TeXmacs/plugins/fish/progs/code/fish-edit.scm @@ -0,0 +1,128 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; MODULE : fish-edit.scm +;; DESCRIPTION : editing fish programs +;; COPYRIGHT : (C) 2025 vesita +;; +;; This software falls under the GNU general public license version 3 or later. +;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +;; in the root directory or . +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; DESCRIPTION: +;;; This module provides editing functionalities for fish language within +;;; TeXmacs. It defines language-specific behaviors such as indentation, +;;; commenting, and paste operations for fish code snippets. +;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 定义模块,使用 prog-edit 和 fish-mode 模块 +(texmacs-module (code fish-edit) + (:use (prog prog-edit) + (code fish-mode))) + +;;------------------------------------------------------------------------------ +;; 缩进设置 +;; + +;; 定义fish代码的制表符停止位为4个空格(符合fish标准) +(tm-define (get-tabstop) + (:mode in-prog-fish?) + 4) + +;; 定义fish特定的关键字,这些关键字后面需要增加缩进 +(define fish-increase-indent-keys + '("define" "if" "loop" "case" "caseof" "section" "command")) + +;; 定义fish特定的配对关键字,这些关键字会减少缩进 +(define fish-decrease-indent-keys + '("else" "case")) + +;; 定义fish特定的结束关键字,这些关键字会减少缩进 +(define fish-end-keys + '("end" "endif" "endloop" "endsection" "endcase" "endcommand")) + +;; 去除字符串右侧的空白字符 +(define (string-strip-right s) + (with char-set:not-whitespace (char-set-complement char-set:whitespace) + (with n (string-length s) + (with r (or (string-rindex s char-set:not-whitespace) n) + (string-take s (min n (+ 1 r))))))) + +;; 检查字符串是否以特定关键字结尾 +(define (ends-with-keyword? s keys) + (and (nnull? keys) + (or (string-ends? s (car keys)) + (ends-with-keyword? s (cdr keys))))) + +;; 检查字符串是否以特定关键字开头 +(define (starts-with-keyword? s keys) + (and (nnull? keys) + (or (string-starts? s (string-append (car keys) " ")) + (starts-with-keyword? s (cdr keys))))) + +;; 定义fish代码的缩进计算函数 +(tm-define (program-compute-indentation doc row col) + (:mode in-prog-fish?) + (if (<= row 0) 0 + (let* ((prev-row (- row 1)) + (prev-line (program-row prev-row)) + (stripped-prev (string-strip-right (if prev-line prev-line ""))) + (prev-indent (string-get-indent stripped-prev)) + (tab-width (get-tabstop))) + (cond + ;; 如果前行以增加缩进的关键字结尾,则当前行应增加缩进 + ((ends-with-keyword? stripped-prev fish-increase-indent-keys) + (+ prev-indent tab-width)) + ;; 如果当前行以减少缩进的关键字开头,则减少缩进 + ((starts-with-keyword? (program-row row) fish-decrease-indent-keys) + (max 0 (- prev-indent tab-width))) + ;; 如果当前行以结束关键字开头,则减少缩进 + ((starts-with-keyword? (program-row row) fish-end-keys) + (max 0 (- prev-indent tab-width))) + ;; 否则保持前行的缩进 + (else prev-indent))))) + +;;------------------------------------------------------------------------------ +;; 自动插入、高亮和选择括号和引号 +;; + +(tm-define (fish-bracket-open lbr rbr) + ;; 插入一对括号或引号,并将光标定位在中间 + (bracket-open lbr rbr "\\")) + +(tm-define (fish-bracket-close lbr rbr) + ;; 处理闭合括号或引号,并正确放置光标位置 + (bracket-close lbr rbr "\\")) + +(tm-define (notify-cursor-moved status) + (:require prog-highlight-brackets?) + (:mode in-prog-fish?) + ;; 当光标移动时高亮匹配的括号 + (select-brackets-after-movement "([{" ")]}" "\\")) + +;;------------------------------------------------------------------------------ +;; 粘贴操作 +;; + +;; 定义fish代码环境中的粘贴操作,使用fish格式导入剪贴板内容 +(tm-define (kbd-paste) + (:mode in-prog-fish?) + (clipboard-paste-import "fish" "primary")) + +(kbd-map + (:mode in-prog-fish?) + ;; fish编程模式下的键盘快捷键 + ("A-tab" (insert-tabstop)) ;; Alt+Tab:插入制表符 + ("cmd S-tab" (remove-tabstop)) ;; Cmd+Shift+Tab:移除制表符 + ("{" (fish-bracket-open "{" "}" )) ;; 自动插入匹配的大括号 + ("}" (fish-bracket-close "{" "}" )) ;; 处理闭合大括号 + ("(" (fish-bracket-open "(" ")" )) ;; 自动插入匹配的小括号 + (")" (fish-bracket-close "(" ")" )) ;; 处理闭合小括号 + ("[" (fish-bracket-open "[" "]" )) ;; 自动插入匹配的方括号 + ("]" (fish-bracket-close "[" "]" )) ;; 处理闭合方括号 + ("\"" (fish-bracket-open "\"" "\"" )) ;; 自动插入匹配的双引号 + ("'" (fish-bracket-open "'" "'" ))) ;; 自动插入匹配的单引号 diff --git a/TeXmacs/plugins/fish/progs/code/fish-lang.scm b/TeXmacs/plugins/fish/progs/code/fish-lang.scm new file mode 100644 index 0000000000..16d49a43e8 --- /dev/null +++ b/TeXmacs/plugins/fish/progs/code/fish-lang.scm @@ -0,0 +1,131 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; MODULE : fish-lang.scm +;; DESCRIPTION : fish language support +;; COPYRIGHT : (C) 2025 veista +;; +;; This software falls under the GNU general public license version 3 or later. +;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +;; in the root directory or . +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; DESCRIPTION: +;;; This module provides language support for fish within TeXmacs. It +;;; defines language features such as keywords, operators, number formats, +;;; string formats, and comment formats for proper syntax highlighting and +;;; parsing of fish code. +;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 定义fish语言支持模块,使用默认语言支持模块 +(texmacs-module (code fish-lang) + (:use (prog default-lang))) + +;;------------------------------------------------------------------------------ +;; 关键字定义 +;; + +;; 定义fish语言的关键字分类 +(tm-define (parser-feature lan key) + (:require (and (== lan "fish") (== key "keyword"))) + `(,(string->symbol key) + (constant + "true" "false" "null") + (declare_function + "define" "struct" "structure") + (declare_identifier + "local" "global") + (keyword_conditional + "if" "else if" "else" "endif" + "caseof" "case" "endcase") + (keyword_control + "loop" "endloop" "exit loop" "continue" + "section" "endsection" "exit section" + "command" "endcommand" + "return" "exit" "end" "lock"))) + +;;------------------------------------------------------------------------------ +;; 操作符定义 +;; + +;; 定义fish语言的操作符符号 +(tm-define (parser-feature lan key) + (:require (and (== lan "fish") (== key "operator"))) + `(,(string->symbol key) + (operator + ;; 算术运算符 + "+" "-" "*" "/" "^" + ;; 关系运算符 + "==" "!=" "#" "<" ">" "<=" ">=" + ;; 逻辑运算符 + "and" "or" "not" + ;; 其他运算符 + "=" "(" ")" "[" "]" "{" "}" ";" "," "." "@"))) + +;;------------------------------------------------------------------------------ +;; 数字格式定义 +;; + +;; 定义fish语言的数字格式,支持科学计数法 +(tm-define (parser-feature lan key) + (:require (and (== lan "fish") (== key "number"))) + `(,(string->symbol key) + (bool_features + "prefix_0x" + "sci_notation"))) + +;;------------------------------------------------------------------------------ +;; 字符串格式定义 +;; + +;; 定义fish语言的字符串格式,支持转义字符 +(tm-define (parser-feature lan key) + (:require (and (== lan "fish") (== key "string"))) + `(,(string->symbol key) + (bool_features + "escape_char_after_backslash") + (escape_sequences "\\" "\"" "n" "t" "b" "r" "f" "a" "v" "0") + (double_escape "'"))) + +;;------------------------------------------------------------------------------ +;; 注释格式定义 +;; + +;; 定义fish语言的注释格式,使用 ; 作为注释 +(tm-define (parser-feature lan key) + (:require (and (== lan "fish") (== key "comment"))) + `(,(string->symbol key) + (inline ";"))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Preferences for syntax highlighting +;; 语法高亮偏好设置 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 通知fish语法更改的函数 +(define (notify-fish-syntax var val) + (syntax-read-preferences "fish")) + +;; 定义fish语法高亮颜色偏好设置 +;; 已验证可生效 +(define-preferences + ("syntax:fish:none" "red" notify-fish-syntax) + ("syntax:fish:comment" "brown" notify-fish-syntax) + ("syntax:fish:error" "dark red" notify-fish-syntax) + ("syntax:fish:constant" "#4040c0" notify-fish-syntax) + ("syntax:fish:constant_number" "#3030b0" notify-fish-syntax) + ("syntax:fish:constant_string" "dark grey" notify-fish-syntax) + ("syntax:fish:constant_char" "#333333" notify-fish-syntax) + ("syntax:fish:declare_function" "#0000c0" notify-fish-syntax) + ("syntax:fish:declare_type" "#0000c0" notify-fish-syntax) + ("syntax:fish:declare_module" "#0000c0" notify-fish-syntax) + ("syntax:fish:operator" "#8b008b" notify-fish-syntax) + ("syntax:fish:operator_openclose" "#B02020" notify-fish-syntax) + ("syntax:fish:operator_field" "#888888" notify-fish-syntax) + ("syntax:fish:operator_special" "orange" notify-fish-syntax) + ("syntax:fish:keyword" "#309090" notify-fish-syntax) + ("syntax:fish:keyword_conditional" "#309090" notify-fish-syntax) + ("syntax:fish:keyword_control" "#008080ff" notify-fish-syntax)) diff --git a/TeXmacs/plugins/fish/progs/code/fish-mode.scm b/TeXmacs/plugins/fish/progs/code/fish-mode.scm new file mode 100644 index 0000000000..8d4f54d49e --- /dev/null +++ b/TeXmacs/plugins/fish/progs/code/fish-mode.scm @@ -0,0 +1,36 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; MODULE : fish-mode.scm +;; DESCRIPTION : fish language mode +;; COPYRIGHT : (C) 2025 vesita +;; +;; This software falls under the GNU general public license version 3 or later. +;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +;; in the root directory or . +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; DESCRIPTION: +;;; This module defines the fish language mode within TeXmacs. It sets up +;;; the environment to detect when the user is working with fish code and +;;; provides appropriate mode detection predicates. +;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 定义fish语言模式模块,使用基础的texmacs模式功能 +(texmacs-module (code fish-mode) + (:use (kernel texmacs tm-modes))) + +;;------------------------------------------------------------------------------ +;; 模式定义 +;; + +;; 定义fish相关的模式谓词 +;; in-fish% 检测当前环境是否为fish编程语言环境 +;; in-prog-fish% 检测是否在程序模式下的fish代码环境中 +(texmacs-modes + (in-fish% (== (get-env "prog-language") "fish")) + ;; 判断是否处于fish代码模式 + (in-prog-fish% #t in-prog% in-fish%)) diff --git a/TeXmacs/plugins/fish/progs/data/fish.scm b/TeXmacs/plugins/fish/progs/data/fish.scm new file mode 100644 index 0000000000..d5db0e421d --- /dev/null +++ b/TeXmacs/plugins/fish/progs/data/fish.scm @@ -0,0 +1,68 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; MODULE : fish.scm +;; DESCRIPTION : prog format for fish +;; COPYRIGHT : (C) 2022-2025 Darcy Shen, Joris van der Hoeven +;; +;; This software falls under the GNU general public license version 3 or later. +;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +;; in the root directory or . +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; DESCRIPTION: +;;; This module defines the format conversion for fish files. It specifies +;;; how to convert between TeXmacs document tree format and fish source +;;; code format, allowing users to import and export fish code. +;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 定义fish格式模块 +(texmacs-module (data fish)) + +;;------------------------------------------------------------------------------ +;; 格式定义 +;; + +;; 定义fish格式,指定名称和文件后缀 +(define-format fish + (:name "fish source code") + (:suffix "fish")) + +;;------------------------------------------------------------------------------ +;; 转换函数定义 +;; + +;; 定义从TeXmacs格式转换为fish格式的函数 +(define (texmacs->fish x . opts) + (texmacs->verbatim x (acons "texmacs->verbatim:encoding" "SourceCode" '()))) + +;; 定义从fish格式转换为TeXmacs格式的函数 +(define (fish->texmacs x . opts) + (code->texmacs x)) + +;; 定义从fish代码片段转换为TeXmacs格式的函数 +(define (fish-snippet->texmacs x . opts) + (code-snippet->texmacs x)) + +;;------------------------------------------------------------------------------ +;; 转换器注册 +;; + +;; 注册TeXmacs文档树到fish文档的转换器 +(converter texmacs-tree fish-document + (:function texmacs->fish)) + +;; 注册fish文档到TeXmacs文档树的转换器 +(converter fish-document texmacs-tree + (:function fish->texmacs)) + +;; 注册TeXmacs文档树到fish代码片段的转换器 +(converter texmacs-tree fish-snippet + (:function texmacs->fish)) + +;; 注册fish代码片段到TeXmacs文档树的转换器 +(converter fish-snippet texmacs-tree + (:function fish-snippet->texmacs)) diff --git a/TeXmacs/tests/tmu/209_20.tmu b/TeXmacs/tests/tmu/209_20.tmu new file mode 100644 index 0000000000..51e8e8e8cb --- /dev/null +++ b/TeXmacs/tests/tmu/209_20.tmu @@ -0,0 +1,1668 @@ +> + +> + +<\body> + [Updated on 3.19] + + Line Code Example : + + \; + + + + Bar Code Example : + + \; + + <\fish-code> + fish define calc_stress(depth, gamma) + + \ \ \ \ local pore = 9810.0 * depth + + \ \ \ \ local total = gamma * depth + + \ \ \ \ local eff = total - pore + + \ \ \ \ io.out('Force:' + string(eff) + ' Pa') + + \ \ \ \ calc_stress = eff + + end + + + [Updated on 3.20] + + Code 1 + + <\fish-code> + model new + + \; + + fish define fname + + \ \ \ \ local a=2, b=3 + + \ \ \ \ global ab=a+b + + end + + \; + + [fname] \ \ \ ; return 0 + + [ab] \ \ \ \ \ \ ; return 5 + + \; + + program return + + + Code 2 + + <\fish-code> + model new fish define fname(a,b) + + \ \ \ \ global ab=a+b + + end + + \; + + [fname(2,3)] ; \ return 0 + + [ab] \ \ \ \ \ \ \ \ ; \ return 5 + + \; + + program return + + + Code 3 + + <\fish-code> + model new + + \; + + fish define derive(y_mod,p_ratio) + + \ \ \ \ global s_mod=y_mod/(2.0*(1.0+p_ratio)) + + \ \ \ \ global b_mod=y_mod/(3.0*(1.0-2.0*p_ratio)) + + end + + \; + + [derive(5e8,0.25)] + + \; + + zone create brick size (2,2,2) + + zone cmodel assign elastic + + zone property bulk [b_mod] shear [s_mod] + + \; + + program return + + + Code 4 + + <\fish-code> + model new + + \; + + ;利用return语句 + + fish define fname(a,b) + + \ \ \ \ return a+b + + end + + [fname(2,3)] ; 5 + + \; + + ;对函数名直接赋值 + + fish define fname(a,b) + + \ \ \ \ global fname=a+b + + end + + [fname(2,3)] ; 5 + + \; + + program return + + + Code 5 + + <\fish-code> + model new + + \; + + fish define long_sum + + \ \ \ \ local a1=1,a2=2,a3=3,a4=4,a5=5,a6=6,a7=7,a8=8,a9=9,a10=10 + + \; + + \ \ \ \ ;设置中间变量temp + + \ \ \ \ local temp = a1+a2+a3+a4+a5 + + \ \ \ \ global long_sum = temp+a6+a7+a8+a9+a10 + + \ \ \ \ \ \ \ \ ;利用续航符 “...” + + \ \ \ \ global long_sum = a1+a2+a3+a4+a5+... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ a6+a7+a8+a9+a10 + + end + + \; + + [long_sum] + + \; + + program return + + + Code 6 + + <\fish-code> + model new + + \; + + fish define fname + + \ \ \ \ command + + \ \ \ \ \ \ \ \ zone create brick size 2 2 2 + + \ \ \ \ endcommand + + end + + \; + + [fname] + + \; + + program return + + + Code 7 + + <\fish-code> + model new + + \; + + fish define fname + + \ \ \ \ local a=2, b=3 + + \ \ \ \ global ab=a+b + + \ \ \ \ io.out(ab) ; 5 + + \; + + end + + [fname] ; 0(没有返回值,自动返回0) + + \; + + program return + + + Code 8 + + <\fish-code> + model new + + \; + + fish define fname + + \ \ \ \ local a=io.in("input a ") + + \ \ \ \ local b=io.in("input b ") + + \ \ \ \ global ab=a+b + + \ \ \ \ io.out(ab) + + end + + \; + + [fname] ; 0(没有返回值,自动返回0) + + \; + + program return + + + Code 9 <\fish-code> + ;主文件 main.f3dat + + model new + + \; + + program call "fish" + + program call "initial" + + \; + + program return + + + Code 10 + + <\fish-code> + ;fish文件 fish.f3dat + + fish define derive(y_mod,p_ratio) + + \ \ \ \ global s_mod=y_mod/(2.0*(1.0+p_ratio)) + + \ \ \ \ global b_mod=y_mod/(3.0*(1.0-2.0*p_ratio)) + + end + + [derive(5e8,0.25)] + + + Code 11 + + <\fish-code> + ;initial.f3dat + + zone create brick size (2,2,2) ;建立模型,创建一个2X2X2的网格 + + zone cmodel assign elastic ;指定材料 + + zone property bulk [b_mod] shear [s_mod] ;赋予材料参数 + + ;未完待续 + + + Code 12 + + <\fish-code> + model new + + fish define test + + \ \ \ \ local a=vector(1,2,3) ; 定义一个向量类型的变量a + + \ \ \ \ local aa=tensor(1,2,3,4,5,6) ; 定义一个张量类型的变量aa + + \ \ \ \ local b=a-\x + <|fish-code> + x ; x代表vector变量的第一个元素 local c=a- + <|fish-code> + z ; z代表vector变量的第三个元素 local bb=aa- + <|fish-code> + xy ; xy代表tensor变量的第四个元素 local cc=aa- + <|fish-code> + xz ; xz代表tensor变量的第五个元素 a- + <|fish-code> + z=4 ; 利用成员访问运算符修改第三个元素 aa- + <|fish-code> + xz=7 ; 利用成员访问运算符修改第五个元素 io.out("b="+string(b)) ; b=1 io.out("c="+string(c)) ; c=3 io.out("bb="+string(bb)) ; bb=4 io.out("cc="+string(cc)) ; cc=5 io.out("a="+string(a)) ; a=(1,2,4) io.out(aa- + <|fish-code> + xz) ; 7 end + + [test] + + program return + + + Code 13 + + <\fish-code> + model new + + \; + + fish define string_test + + \; + + \ \ \ \ a = "Itasca" + + \ \ \ \ \ \ \ \ ; 索引 + + \ \ \ \ b = a(2) ; b = "t" + + \ \ \ \ c = a(-3) ; c = "s" + + \ \ \ \ d = a(2,4) ; d = "tas",满足双索引第一条 + + \ \ \ \ e = a(-3,-1) ; e = "sca",满足双索引第一条 + + \ \ \ \ f = a(3,-4) ; f = "a", a(1,3)返回"Ita",a(-4,-1)返回"asca",重叠字符为“a”,数量为1 + + \ \ \ \ g = a(3,-5) ; g = "", a(1,3)返回"Ita",a(-5,-1)返回"tasca",重叠字符为“ta”,数量为2 + + \ \ \ \ io.out(b+"n"+c+"n"+d+"n"+e+"n"+f+"n"+g) + + \; + + \ \ \ \ ; 修改字符串内容 + + \ \ \ \ a(2) = "XA" io.out(a+"n") ; a= "IXAasca" + + \ \ \ \ a(-2,-1) = "XA" io.out(a+"n") ; a= "IXAascXA" + + \ \ \ \ a(-3,-1) = "XA" ; 修改字符长度大于替换字符长度, + + \ \ \ \ io.out(a+"n") ; a= "IXAasXA" + + \ \ \ \ a(-1,-1) = "XA" ; 修改字符长度小于替换字符长度 + + \ \ \ \ io.out(a+"n") ; a= "IXAasXXA" + + end + + \; + + [string_test] ;执行函数 + + \; + + program return + + + Code 14 + + <\fish-code> + model new + + \; + + fish define in_def(msg,default) + + \ \ \ \ local xx = io.in(msg+'('+'default:'+string(default)+'):') + + \ \ \ \ if type(xx) = 3 + + \ \ \ \ \ \ \ \ in_def = default + + \ \ \ \ else + + \ \ \ \ \ \ \ \ in_def = xx + + \ \ \ \ endif + + end + + def moduli_data + + \ \ \ \ global y_mod = in_def('Input Young`s modulus ', 1.0e9) + + \ \ \ \ global p_ratio = in_def('Input Poisson`s ratio ', 0.25) + + \ \ \ \ if p_ratio = 0.5 then + + \ \ \ \ \ \ \ \ io.out(' Bulk mod is undefined at Poisson`s ratio = 0.5') + + \ \ \ \ \ \ \ \ io.out(' Select a different value --') + + \ \ \ \ \ \ \ \ p_ratio = in_def + + \ \ \ \ endif + + \ \ \ \ global s_mod = y_mod / (2.0 * (1.0 + p_ratio)) + + \ \ \ \ global b_mod = y_mod / (3.0 * (1.0 - 2.0 * p_ratio)) + + end + + \; + + [moduli_data] + + [y_mod] + + [p_ratio] + + [s_mod] + + [b_mod] + + \; + + program return + + + Code 15 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ local a=math.random.uniform() + + \ \ \ \ io.out(string(a, 6,' ',3,'f')) ; 小数点后3位,小数点1位,小数点前占1位, + + \ \ \ \ io.out(string(a, 9,' ',3,'e')) ; 小数点后3位,小数点1位,小数点前占1位, + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 16 + + <\fish-code> + ;建立一个3X3X3的网格,利用循环语句和指针打印单元号,及每一个单元形心的位置 + + model new + + \; + + ;模型 + + zone create brick size 3 3 3 + + fish define test + + \ \ \ \ loop foreach _pnt zone.list + + \ \ \ \ \ \ \ \ io.out(_pnt) + + \ \ \ \ \ \ \ \ io.out("zone id="+string(zone.id(_pnt))+" "+"its centroid position"+" "... +string(zone.pos(_pnt))) \ \ \ \ \ endloop + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 17 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ local a=(1,2,3) + + \ \ \ \ local b=(3,2,1) + + \ \ \ \ local aa=a+b + + \ \ \ \ local bb=a*b + + \ \ \ \ local cc=a*5 + + \ \ \ \ local dd=b/5 + + \ \ \ \ io.out(aa) ; (4,4,4) + + \ \ \ \ io.out(bb) ; (3,4,3) + + \ \ \ \ io.out(cc) ; (5,10,15) + + \ \ \ \ io.out(dd) ; (0.6,0.4,0.2) + + end + + \; + + [test] + + \; + + program return + + + Code 18 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ local a=(1,2,3) + + \ \ \ \ loop foreach local com a + + \ \ \ \ \ \ \ \ io.out(com) + + \ \ \ \ endloop + + end + + \; + + [test] + + \; + + program return + + + Code 19 + + <\fish-code> + model new + + \; + + zone create brick size 2 2 2 + + zone cmodel assign elastic + + zone property bulk 2e9 shear 6e8 + + \; + + ;通过分割进行操作 + + ;[zone.prop(::zone.list,'shear') *= 0.5 + + ;不再需要for loopeach...endloop结构进行循环 + + \; + + ;通过循环操作 fish define loop_2_modify + + \ \ \ \ loop foreach _pnt zone.list + + \ \ \ \ \ \ \ \ zone.prop(_pnt,'shear') *= 0.5 + + \ \ \ \ endloop end + + \; + + [loop_2_modify] + + \; + + program return + + + Code 20 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ local a=list ;创建空列表 + + \; + + \ \ \ \ ; 创建元素相同的列表 + + \ \ \ \ local b=list.create(10);创建一个长度为10的列表,元素默认为0 + + \ \ \ \ local c=list.create(10,1);创建一个长度为10的列表,元素都为1 + + \ \ \ \ local d=list.create(10,'a');创建一个长度为10的列表,元素都为'a' + + \; + + \ \ \ \ local e=list.sequence(1,2,'a',(1,2),math.pi) ;按顺序创建列表,元素类型可以不同 + + \; + + \ \ \ \ local f=list.range(1,10) ;按元素为等差数列创建列表,间隔为默认为1 + + \ \ \ \ local g=list.range(1,10,2) ;按元素为等差数列创建列表,间隔为2 + + \ \ \ \ local h=list.range(10,1,-1) ;按元素为等差数列创建列表,间隔可为负值,该例中为-1 + + \; + + \ \ \ \ io.out(list.size(h)) ; 输出列表的大小,此处为10 + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 21 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ local a=list.range(1,10) ; {1,2,3,4,5,6,7,8,9,10} + + \; + + \ \ \ \ local b=list.append(a,11) ; {1,2,3,4,5,6,7,8,9,10,11} + + \ \ \ \ \ \ \ \ local c=list.append(a,(11, 12)) ; {1,2,3,4,5,6,7,8,9,10,(11,12)} 不展开内容 + + \ \ \ \ \ \ \ \ local d=list.extend(a,(11, 12)) ; {1,2,3,4,5,6,7,8,9,10,11,12} 展开内容 + + \; + + \ \ \ \ local e=list.prepend(11,a) ; {11, 1,2,3,4,5,6,7,8,9,10} + + \ \ \ \ \ \ \ \ local f=list.prepend((11,12),a) ; {(11,12), 1,2,3,4,5,6,7,8,9,10} 不展开内容 + + \ \ \ \ \ \ \ \ ; 插入单值 + + \ \ \ \ \ \ \ \ local g=list.insert(a,3,'a') ; {1,2,'a',3,4,5,6,7,8,9,10} + + \ \ \ \ \ \ \ \ ; 插入一个变量 + + \ \ \ \ \ \ \ \ local h=list.insert(a,3, a) ; {1,2,Size 10,3,4,5,6,7,8,9,10} + + \; + + \ \ \ \ ; 插入多值 + + \ \ \ \ \ \ \ \ local i=list.insert.list(a,3,list.sequence('a',1));{1,2,'a',1,3,4,5,6,7,8,9,10} + + \ \ \ \ end + + \; + + [test] + + \; + + program return + + + Code 22 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ ;利用列表的range方法创建列表,其中元素按等差数列分布 + + \ \ \ \ \ \ \ \ local a = list.range(1,10) + + \ \ \ \ \ \ \ \ local b = list.range(20,11,-1) + + \; + + \ \ \ \ local d = a * 2 ;; 2是整数(非列表),d中元素是a中对应元素乘以2,相当于d=list.range(2,20,2) + + \ \ \ \ \ \ \ \ local e = a + b ;; e中所有元素的值都是21 + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 23 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ ; 建立一个元素是向量的列表 + + \ \ \ \ \ \ \ \ local a = list.sequence( (1,2,3) , ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (4,5,6) , ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (7,8,9) , ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (10,11,12) ) + + \ \ \ \ ; 将列表中每一个元素,即每一个向量的第二元素取出,形成一新的列表local b = a-y + + ; 将列表中每一个元素,即每一个向量的第三个元素除以2,形成一新的列表 a-z /= 2 + + end + + [test] + + program return + + + Code 24 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ local a = tensor(2,3,4,1,6,5) + + \ \ \ \ local c = a(5) ; c = 6.0 + + \ \ \ \ local d = a(1,3) ; d = 6.0 + + \ \ \ \ local e = a-xz ; e = 6.0 + + a(5) = 6.0 ; 下列三行代码效果一样,都是修改张量xz分量的值 + + a(1,3) = 6.0 a-xz = 6.0 + + end + + [test] + + program return + + + Code 25 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ ;前两个参数是整数,代表矩阵的大小 + + \ \ \ \ global m3 = matrix(3,3, ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1,2,3, ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 4,5,6, ... + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 7,8,9) + + \ \ \ \ global m1 = matrix(1,3,1,2,3) + + \ \ \ \ global a = m3(2,3) ; a = 6.0 + + \ \ \ \ m3(2,3) = 6.5 ; m3 = ((1,2,3) (4,5,6.5) (7,8,9)) + + \ \ \ \ global b = m1(3) ; b = 3.0 + + \ \ \ \ m1(3) = 3.5 + + \; + + \ \ \ \ ;利用四个指标,选取块矩阵 + + \ \ \ \ global c = m3(2,1,2,3) ; A 1x3 row vector removed from m3 (4,5,6.5) + + \ \ \ \ ;更新选取的块矩阵的内容 + + \ \ \ \ m3(3,1,3,3) = matrix(1,3,10,11,12) ; m3 = ((1,2,3) (4,5,6.5) (10,11,12)) + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 26 + + <\fish-code> + model new + + \; + + fish define setup + + \ \ \ \ alan = matrix(4,4) ; 大小为4X4的矩阵,元素为0 + + \ \ \ \ alan(1,3) = 6 + + \ \ \ \ alan(2,1) = 2.2 + + \ \ \ \ alan(3,4) = 6.6 + + \ \ \ \ alan(4,2) = math.pi end [setup] [alan(4,2)] ;math.pi [alan = matrix.transpose(alan)] ;原矩阵的转置 [alan(2,4)] ;math.pi + + \; + + fish define sum_tests + + \ \ \ \ herp = matrix(2,2, 6,-1,-3,2) + + \ \ \ \ derp = matrix(2,2, 4, 3,-7,0) + + \ \ \ \ \ \ \ \ klerp = herp + derp + + \ \ \ \ werp = herp - derp + + \ \ \ \ serp = herp * derp + + end + + [sum_tests] + + [klerp(1,1)] ; 10 + + [werp(1,1)] ; 2 + + [serp(1,1)] ; 31 + + \; + + fish define test + + \ \ \ \ local first = vector(2,4,3) + + \ \ \ \ local second = vector(8,1,5) + + \ \ \ \ local prod = math.outer.product(first, second) ; 并积 + + \ \ \ \ local output = list(prod) + + end + + [test] + + fish list contents [output] ; 输出列表output的内容 + + \; + + program return + + + Code 27 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ local a=map() ;空映射 + + \ \ \ \ local b=map(10, 1.0,'fred',4.0,3.4,6.0) ;通过键值对形成映射 + + \; + + \ \ \ \ ;通过元素相同的两个列表创建映射 + + \ \ \ \ local c=map(list.sequence(1,'a',(1,2)),list.sequence('b',1,'b') + + end + + \; + + [test] + + \; + + program return + + + Code 28 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ map1 = map(10,1.0,'fred',4.0,3.4,6.0) + + \; + + \ \ \ \ ; 所有映射中的所有值 + + \ \ \ \ loop foreach local com map1 + + \ \ \ \ \ \ \ \ io.out(com) + + \ \ \ \ endloop + + \; + + \ \ \ \ ; 对列表中所有键进行循环,并通过键获取值 + + \ \ \ \ loop foreach local com1 map.keys(map1) + + \ \ \ \ \ \ \ \ \ io.out('key= '+string(com1)+'t'+'value= '+string(map1(com1))) + + \ \ \ \ endloop + + \; + + end + + \; + + [test] + + \; + + program return + + + Code 30 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ local a = map(1,'fred',5,'george') + + \ \ \ \ local b = a ; 创建a的副本,相当于在内存地址中重新开辟一个空间存储同一个对象 + + \; + + \ \ \ \ ; 修改副本对象的内容不会影响原对象,同样修改原对象也不会影响副本对象 + + \ \ \ \ b(5) = 'mary' + + \ \ \ \ io.out(a(5)) ; george + + \ \ \ \ io.out(b(5)) ; mary + + \ \ \ \ end + + \; + + [test] + + \; + + program return + + + Code 31 + + <\fish-code> + model new + + \; + + fish define testBreak + + \; + + \ \ \ \ local testmap = map('first',1, 'second',2, 'third',3) + + \ \ \ \ global sum = 0 + + \ \ \ \ loop foreach local v testmap ;对值进行循环 + + \ \ \ \ \ \ \ \ sum = sum + v + + \ \ \ \ endloop + + \; + + end [testBreak] [sum] ; 6 + + \; + + fish define test1 + + \ \ \ \ local temp = map('hello',1,'world',2) + + \ \ \ \ global test1 = temp('hello') + temp('world') ;映射的值求和 end [test1] ; 3 + + \; + + fish define test2 + + \ \ \ \ local testmap = map('first',1,'second',2,'third',3) + + \ \ \ \ map.remove(testmap,'first') + + \ \ \ \ map.add(testmap,'fourth',4) ; 键值对增加映射元素 + + \ \ \ \ testmap('second')=8 ; 根据键修改值 + + \ \ \ \ local output = 0 + + \ \ \ \ loop foreach local n testmap ; 映射值求和 + + \ \ \ \ \ \ \ \ output = output + n + + \ \ \ \ endloop + + \ \ \ \ test2 = output end [test2] ; 15 + + \; + + fish define test3 + + \ \ \ \ ; test keys + + \ \ \ \ testmap = map('yet',1,'another',4,'test',9,'m',16) + + \ \ \ \ testkeys = map.keys(testmap) ; list.sequence('yet', 'another', 'test', 'm') + + \ \ \ \ test3 = testkeys(3) ; 'test' + + \; + + \ \ \ \ if (map.has(testmap,'wrongkey')) ; map.has() 判断映射中是否有相应的key + + \ \ \ \ \ \ \ \ hascheck = 909 + + \ \ \ \ else if (map.has(testmap,'another')) + + \ \ \ \ \ \ \ \ hascheck = testmap('another') ; 根据键赋值 + + \ \ \ \ endif end [test3] [hascheck] + + \; + + program return + + + Code 32 + + <\fish-code> + model new + + \; + + model random 10000 ; 这个命令放在moded new后面,保证每次生成树的随机数是一样的 + + \; + + fish define afill ; 用随机数填充矩阵 + + \ \ \ \ global var = array.create(4,3) + + \ \ \ \ loop local m (1,array.size(var,1)) + + \ \ \ \ \ \ \ \ loop local n (1,array.size(var,2)) + + \ \ \ \ \ \ \ \ \ \ \ \ var(m,n) = math.random.uniform + + \ \ \ \ \ \ \ \ endloop + + \ \ \ \ endloop + + \; + + \ \ \ \ global var2 = array(math.random.uniform(4*3),4,3) end + + \; + + fish define ashow(v) ; 显示矩阵元素 + + \ \ \ \ loop local m (1,array.size(v,1)) + + \ \ \ \ \ \ \ \ local hed = ' ' + + \ \ \ \ \ \ \ \ local msg = ' '+string(m) + + \ \ \ \ \ \ \ \ loop local n (1,array.size(v,2)) + + \ \ \ \ \ \ \ \ \ \ \ \ hed = hed + ' '+string(n) + + \ \ \ \ \ \ \ \ \ \ \ \ msg = msg + ' '+string(v(m,n),8,' ',8,'e') + + \ \ \ \ \ \ \ \ endloop + + \ \ \ \ \ \ \ \ if m = 1 + + \ \ \ \ \ \ \ \ \ \ \ \ io.out(hed) + + \ \ \ \ \ \ \ \ endif + + \ \ \ \ \ \ \ \ \ \ \ \ io.out(msg) + + \ \ \ \ endloop end + + \; + + [afill] [ashow(var)] [ashow(var2)] + + \; + + program return + + + Code 33 + + <\fish-code> + model new + + \; + + ;利用fish structure 命令在函数外定义结构体,结构体有三个变量 fish structure fred(one,two,three) + + \; + + fish define test + + \ \ \ \ ;利用 structure 语句作为在FISH函数中定义结构体,结构体有三个变量 + + \ \ \ \ structure george(val1,val2,val3) + + \; + + \ \ \ \ ;george结构体的实例化,并赋予初值 + + \ \ \ \ global fred = struct georg e(1,2,3) + + \ \ \ \ \ \ \ \ ;fred结构体的实例化,未赋初值,初值为0 + + \ \ \ \ global mary = struct fred + + \; + + \ \ \ \ ;val1对应的值给test + + \ \ \ \ test = fred-val1 + + mary-two = 'monkey' + + end + + [test] ; 1 + + [mary-one] ; 0 + + [mary-two] ; monkey + + [mary-three] ; 0 + + program return + + + Code 34 + + <\fish-code> + caseof expr + + \ \ \ \ ... ; 默认的代码,即以下条件都不满足时执行 + + \ \ \ \ \ \ \ \ case i1 + + \ \ \ \ \ \ \ \ ... ; 代码块1 + + \ \ \ \ case i2 + + \ \ \ \ \ \ \ \ ... ; 代码块2 + + \ \ \ \ case i3 + + \ \ \ \ \ \ \ \ ... ; 代码块3 + + endcase + + + Code 35 + + <\fish-code> + model new + + \; + + fish define test + + \; + + \ \ \ \ a=io.in("input value of a ") + + \ \ \ \ b=io.in("input value of b ") + + \ \ \ \ operator=io.in("input operator (+ - * /)") + + \; + + \ \ \ \ caseof operator + + \ \ \ \ \ \ \ \ \ \ \ \ io.out("no valide operation is detected!") ; 默认输出,即没有相应的匹配 + + \ \ \ \ \ \ \ \ \ \ \ \ case '+' + + \ \ \ \ \ \ \ \ io.out("the operator indicates addition") + + \ \ \ \ \ \ \ \ io.out("a+b="+string(a+b)) + + \ \ \ \ case '-' + + \ \ \ \ \ \ \ \ io.out("the operator indicates subtraction") + + \ \ \ \ \ \ \ \ io.out("a-b="+string(a-b)) + + \ \ \ \ case '*' + + \ \ \ \ \ \ \ \ io.out("the operator indicates multiplication") + + \ \ \ \ \ \ \ \ io.out("a*b="+string(a*b)) + + \ \ \ \ case '/' + + \ \ \ \ \ \ \ \ io.out("the operator indicates division") + + \ \ \ \ \ \ \ \ io.out("a/b="+string(a/b)) + + \ \ \ \ endcase + + end + + \; + + [test] + + \; + + program return + + + Code 36 + + <\fish-code> + command + \ \ \ \ ... + endcommand + + + <\fish-code> + if expression then + + \ \ \ \ ... + + else if expression then + + \ \ \ \ ... + + else + + \ \ \ \ ... + + endif + + + <\fish-code> + if expression then expression + + + Code 37 + + <\fish-code> + loop var (expr1, expr2, ) + + \ \ \ \ ... + + endloop + + + Code 38 + + <\fish-code> + model new + + \; + + fish define sum(n) + + \ \ \ \ local s = 0 + + \ \ \ \ loop local i (1,n) + + \ \ \ \ \ \ \ \ s += i + + \ \ \ \ endloop + + \ \ \ \ global sum = s end + + \; + + [s = sum(10)] + + fish list [s] ; 55 + + \; + + fish define sum_even(n) + + \ \ \ \ local s = 0 + + \ \ \ \ loop local i (n,0,-2) + + \ \ \ \ \ \ \ \ s += i + + \ \ \ \ endloop + + \ \ \ \ global sum_even = s end + + \; + + [s2 = sum_even(10)] fish list [s2] ; 30 + + \; + + program return + + + Code 39 + + <\fish-code> + loop while exprtest + + \ \ \ \ ... + + endloop + + + Code 40 + + <\fish-code> + model new + + \; + + fish define sum_even(n) + + \ \ \ \ local s = 0 + + \ \ \ \ local i = 0 + + \ \ \ \ loop while i \= n + + \ \ \ \ \ \ \ \ s += i + + \ \ \ \ \ \ \ \ i +=2 + + \ \ \ \ endloop + + \ \ \ \ \ \ \ \ global sum_even = s end + + \; + + [s2 = sum_even(10)] fish list [s2] + + \; + + program return + + + Code 41 + + <\fish-code> + loop for (exprinit, exprtest, exprmodify) + + \ \ \ \ ... + + endloop + + + Code 42 + + <\fish-code> + model new + + \; + + fish define sum_odd(n) + + \ \ \ \ local s= 0 + + \ \ \ \ loop for (local i=1, i \= n, i+=2) + + \ \ \ \ \ \ \ \ s += i + + \ \ \ \ endloop + + \ \ \ \ global sum_odd = s end + + \; + + [s_odd = sum_odd(10)] fish list [s_odd] + + \; + + program return + + + Code 43 + + <\fish-code> + loop foreach var expr1 + + \ \ \ \ ... + + endloop + + + Code 44 + + <\fish-code> + model new + + \; + + fish define test + + \ \ \ \ local a=math.random.uniform(10) ;形成含10个随机数的列表 + + \ \ \ \ loop foreach local com a + + \ \ \ \ \ \ \ \ io.out(com) + + \ \ \ \ endloop end + + \; + + [test] + + \; + + program return + + + Code 46 + + <\fish-code> + model new + + \; + + fish define write2file + + \; + + \ \ \ \ local f=file.open('test.txt', 'w') ; 打开写入的文件 + + \ \ \ \ local val=math.random.uniform(20) ; 由均匀分布的随机数形成的列表 + + \ \ \ \ local l=list ; 空列表 + + \; + + \ \ \ \ loop foreach local com val + + \ \ \ \ \ \ \ \ l=list.append(l,com) ; 随机数插入列表 + + \ \ \ \ endloop + + \; + + \ \ \ \ file.write(f)=l + + \ \ \ \ \ \ \ \ local b=file.close(f) + + end + + \; + + [write2file] + + \; + + fish define read_from_file + + \; + + \ \ \ \ local f=file.open('test.txt', 'r') ; 打开读取的文件 + + \ \ \ \ local l=file.read(f) + + \; + + \ \ \ \ loop foreach local com l + + \ \ \ \ \ \ \ \ io.out(com) ; 输出列表中的元素 + + \ \ \ \ endloop + + \ \ \ \ \ \ \ \ local b=file.close(f) + + \; + + end + + \; + + [read_from_file] + + \; + + program return + + + Code 47 + + <\fish-code> + model new + + \; + + model large-strain off fish automatic-create off + + \; + + model title "Braced support with a beam of a vertical excavation" ; ; ; zone create brick size 10 10 10 + + \; + + zone cmodel assign mohr-coulomb zone property bulk 1e8 shear 0.3e8 friction 35 zone property cohesion 1e10 tension 1e10 density 1000 + + \; + + zone face apply velocity-normal 0 range union position-x 0 position-x 10 zone face apply velocity-normal 0 range union position-y 0 position-y 10 zone face apply velocity-normal 0 range position-z 0 + + \; + + model gravity (0,0,-9.81) model solve ratio-local 1e-5 model save 'initial' ; ; ; model restore 'initial' zone gridpoint initialize displacement (0,0,0) ;位移清零,关注开挖的影响 + + \; + + ; 赋予材料真实值 zone property cohesion 1.25e3 tension 1e3 zone cmodel assign null range position (3,3,6) (7,7,10) model large-strain on + + \; + + ; 历史记录值 history interval 10 zone history name 'disp-x' displacement-x gridpointid 1269 zone history name 'unbalf-x' unbalanced-force-x gridpointid 1269 + + \; + + model step 2000 ; ;采样间隔为10,因此第一个点为1350,第二个点为1360,以此类推 history export 'unbalf-x' vs step file 'test1.txt' truncate + + \; + + ;输出间隔为10,第一个点为1350, skip 10,表示第二个点与第一个点的间隔为100(10X10),即1450,以此类推 history export 'unbalf-x' vs 'disp-x' skip 10 file 'test2.txt' truncate + + \; + + ;输出为table history export 'unbalf-x' vs 'disp-x' table '1' ;更改图例 table '1' label 'f-x' + + \; + + model save 'excavation' + + + +<\initial> + <\collection> + + + + diff --git a/devel/209_20.md b/devel/209_20.md new file mode 100644 index 0000000000..91fc0ff051 --- /dev/null +++ b/devel/209_20.md @@ -0,0 +1,22 @@ +# [209_20] 增加Fish(Itasca)代码块及高亮支持 + +## 如何测试 + +1. 选择菜单插入-程序-代码块/行内代码或使用 `\fish` 或者 `\fish-code`插入代码块环境 +2. 输入一些fish代码检查高亮 +- 测试文档: TeXmacs/tests/tmu/209_20.tmu + +## 文件位置 +- 语言定义:TeXmacs/plugins/fish/progs/data/fish.scm +- 样式包:TeXmacs/plugins/fish/packages/code/fish.ts +- 编辑功能:TeXmacs/plugins/fish/progs/code/fish-edit.scm +- 高亮功能:TeXmacs/plugins/fish/progs/code/fish-lang.scm +- 模式定义:TeXmacs/plugins/fish/progs/code/fish-mode.scm +- 文档:TeXmacs/plugins/fish/doc/fish.en.tmu + +## [Updated on 2026/03/20] + +### What + +按照Fish(Itasca)官网说明,添加了更多的关键字支持。 +提供了更多的Fish(Itasca)代码举例。