0

red 语言初体验

 1 year ago
source link: https://forrestsu.github.io/posts/languages/red-%E8%AF%AD%E8%A8%80%E5%88%9D%E4%BD%93%E9%AA%8C/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

red 语言初体验

2017年10月5日
| 字数 3036

我第一次听到 red 这门语言是在2017-07-20,是通过陈天的一篇关于 red 的文章。正好趁十一,闲得浮生几日,来入个门。下面步入正题: red 目前最新的版本是0.6.3 (2017-10-05)

1 red 语言特性

我们首先来看下 red 语言的一些特性:

  • 1 red 是一门编译型语言,不走虚拟机,直接编译成目标平台的,和 C 代码同级别性能的二进制。编译时可以直接跨平台往 windows / linux / osx 等 target OS,以及 x86 / arm 等 target CPU 上面编译。

  • 2 跨平台的本地GUI 看上去像是一门后端的语言,竟然去抢前端的饭碗。而且,妄图支持 osx,windows,android,iOS,一统江湖。是的,就是这么性感。 突然觉得,如果只想学一门语言,又想成为全栈工程师,red 刚刚好。

  • 3 red 类型树 red 目前提供了大约50多种常用类型,而且能够赋值时自动识别类型。

  • 4 程序小 red 可执行程序只有1.1M左右,cool!

2 尝试做个计算器

光说不练假把式,我们来仿写一个 Apple 计算器。

代码实现:

Red [
    Title: "计算器"
	Author: "ForrestSu"
	Date: 2017-10-04
	Version: 1.0.0
	purpose: "模仿Apple计算器"
	File: 	 %applecalc.red
	Needs:	 'View
]
lite-gray: 220.220.220
white-gray: 240.240.240

; 下面的值保存当前内存中的值
value1: 0         ;上一次计算的结果
value2: 0         ;正在输入的数
last_op: "="      ;上一次按下的键
is_point?: false ;标记是否已经输入过小数点 

view [
    title "计算器"   ;视窗标题
	; 从按键的外观和功能来分析,我把按键分为三类,分别是:
	; * 数字键:十个数字和小数点
	; * 计算键:加、减、乘、除、以及等号
	; * 功能键:「C/AC」、「+/-」、「%」
	; 为此三类我们定义了三个样式,分别是 num_btn,func_btn,op_btn 。num_btn,func_btn,op_btn 。
	style btn: base white-gray 60x50 font-size 16 draw [pen gray box 0x0 60x50]
	style num_btn: btn white-gray[
	    ; 「C/AC」按键上面的文字会在 "AC" 和 "C" 之间切换:
		; * AC 用来清除「当前状态」。
		; * C 用来清除当前画面,使得画面显示0。
		; 当数字键被按下之后,画面上有数字了,「C/AC」键就会显示「C」
		c_btn/text: "C"
	    ; 小数点是否已经被按过
		either not is_point? [
		  display/data: value2: value2 * 10 + load face/text
		][
		  append display/text face/text
		  ;value2此时必须跟著画面一起更新
		  value2: display/data
		]
	]
	; func_btn 是功能键,继承自 btn,改变了颜色,但不定义共同的行为,
    ; 因为每个功能键的行为差异太大。
    style func_btn: btn lite-gray
	
	style op_btn: btn orange font-color white [
	    value2: display/data
		value1: switch last_op [
			"÷" [ value1 * 1.0 / value2 ]
			"x" [ value1 * value2]
			"-" [ value1 - value2]
			"+" [ value1 + value2]
			"=" [ value2]
		]
		if none? value1 [ value1: 0 ]
		; 计算完了是否把 value2 清空 
		value2: 0
		is_point?: false
		last_op: face/text ;记录此次按下的符号
	    display/data: value1
	]
    origin 0x0
	space 0x0
	display: text gray 240x50 font-color white font-size 30 right data 0 
	return 
	c_btn: func_btn "AC" [
	    either "AC" = face/text [
		; 当上面的字样是”AC"时,表示要清除内存,把 is-point?、value1、op、value2
		; 这四个值都恢复到原状。但因为出现 "AC" 表示已经出现过 "C",而处理"C" 的时候
		; 会把 value2 和 is-point? 复原,所以这里只要复原 value1 和 op 即可。
	       value1: 0
           last_op: "="
	    ][
		    display/data: 0
			value2: 0
			is_point?: false
			; 按下"C"之后,上面的字样会变成"AC"
			face/text: "AC"
		]
	]
	func_btn "+/-" [
	   ; 如果第一个字符是减号,就去除减号
	    either #"-" = display/text/1 [
	       remove display/text
		][ insert display/text #"-" ]
	]
	func_btn "%" [
		display/data: display/data / 100.0
	]
	op_btn "÷" return 
	num_btn "7" num_btn "8" num_btn "9" op_btn "x" return
	num_btn "4" num_btn "5" num_btn "6" op_btn "-" return
	num_btn "1" num_btn "2" num_btn "3" op_btn "+" return
	num_btn "0" 120x50 draw [ pen gray box 0x0 120x50 ]
	num_btn "." [
		if not is_point? [
		   append display/text "."
		   is_point?: true
		]
	]
	op_btn "="
]
	

代码去除注释大概80行左右,具体代码在这里就不详细解释了! Amazing, so sexy! 最后编译成可执行程序:

red -t Windows -o red\applecalc.exe red\applecalc.red

最后生成exe可执行文件。(外加一个运行时库 libRedRt.dll)

如果你也感兴趣,可以好好把玩。red 1.0,期待ing…


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK