1374 字
7 分钟
PureEnglish 使用说明
欢迎使用 PureEnglish!这份文档将帮助你学习如何使用这种简单、直观的编程语言。
目录
快速开始
运行你的第一个程序
- 确保你已经按照 构建指南 成功构建了 PureEnglish 解释器
- 创建一个新文件
hello.epp,内容如下:
print "Hello, World!"- 编译程序:
pe hello.epp- 运行程序:
hello.exe恭喜!你已经运行了第一个 PureEnglish 程序!
基础语法
注释
使用 // 开头添加注释,注释会被解释器忽略:
// 这是一个注释print "Hello" // 这也是注释变量
PureEnglish 使用动态类型,变量在第一次赋值时自动创建:
name = "Alice"age = 25price = 19.99is_student = true变量可以随时改变类型:
x = 10 note: 整数x = "hello" note: 现在是字符串数据类型
PureEnglish 支持以下数据类型:
整数 (int)
count = 42score = -10浮点数 (float)
pi = 3.14159temperature = -5.5字符串 (string)
greeting = "Hello, World!"empty = ""字符 (char)
letter = 'A'digit = '7'布尔值 (bool)
is_active = trueis_done = false运算符
算术运算符
a = 10b = 3
sum = a + b note: 加法,结果 13diff = a - b note: 减法,结果 7product = a * b note: 乘法,结果 30quotient = a / b note: 除法,结果 3.333...remainder = a % b note: 取模,结果 1比较运算符
x = 10y = 20
x = y note: 等于(注意:用单等号)x != y note: 不等于x > y note: 大于x < y note: 小于x >= y note: 大于等于x <= y note: 小于等于逻辑运算符
a = trueb = false
a and b note: 与,结果 falsea or b note: 或,结果 truenot a note: 非,结果 false控制流
if 语句
age = 18
if age >= 18 then print "You are an adult"end if带 else 的 if 语句:
score = 85
if score >= 90 then print "Grade: A"else if score >= 80 then print "Grade: B"else if score >= 70 then print "Grade: C"else print "Grade: F"end ifwhile 循环
i = 1
while i <= 5 do print "Count:", i i = i + 1end whilerepeat 循环
重复指定次数:
repeat 3 times print "Hello!"end repeatfor 循环
范围循环:
for i from 1 to 5 print iend for带步长的 for 循环:
note: 从 10 倒数到 1for i from 10 to 1 step -1 print iend for函数
定义函数
使用 function 关键字定义函数,takes 后面跟参数列表:
function greet takes name print "Hello,", nameend function
function add takes a, b return a + bend function调用函数
greet("Alice")
result = add(5, 3)print result note: 输出 8完整的函数示例
function factorial takes n if n <= 1 then return 1 else return n * factorial(n - 1) end ifend function
print factorial(5) note: 输出 120标准库函数
输出内容到控制台:
print "Hello"print "The answer is", 42print 1, 2, 3, 4, 5input
从控制台获取用户输入:
input "What's your name?" into nameprint "Hello,", namelength
获取字符串长度:
text = "Hello"len = length(text)print len note: 输出 5to_number
将字符串转换为数字:
str = "123"num = to_number(str)print num + 1 note: 输出 124to_string
将数字转换为字符串:
num = 42str = to_string(num)print "Number: " + strrandom
生成指定范围内的随机整数:
note: 生成 1 到 10 之间的随机数dice = random(1, 10)print "Random number:", dicesleep
暂停程序执行指定秒数:
print "Waiting..."sleep(2) note: 等待 2 秒print "Done!"exit
退出程序,可指定退出代码:
exit(0) note: 正常退出exit(1) note: 错误退出完整示例
示例 1: 简单计算器
note: 简单的计算器程序
print "PureEnglish Calculator"print "----------------------"
input "Enter first number: " into a_strinput "Enter operator (+, -, *, /): " into opinput "Enter second number: " into b_str
a = to_number(a_str)b = to_number(b_str)
if op = "+" then result = a + belse if op = "-" then result = a - belse if op = "*" then result = a * belse if op = "/" then result = a / belse print "Unknown operator!" exit(1)end if
print "Result:", result示例 2: 猜数字游戏
note: 猜数字游戏
print "Welcome to Guess the Number!"print "I'm thinking of a number between 1 and 100"
secret = random(1, 100)attempts = 0
while true do input "Take a guess: " into guess_str guess = to_number(guess_str) attempts = attempts + 1
if guess < secret then print "Too low!" else if guess > secret then print "Too high!" else print "Congratulations! You guessed it in", attempts, "attempts!" exit(0) end ifend while示例 3: 斐波那契数列
note: 计算斐波那契数列
function fib takes n if n <= 1 then return n else return fib(n - 1) + fib(n - 2) end ifend function
print "Fibonacci sequence:"for i from 0 to 10 print "fib(", i, ") =", fib(i)end for示例 4: 冒泡排序
note: 冒泡排序算法(使用数组模拟)
print "Bubble Sort Demo"print "----------------"
note: 我们将使用多个变量来模拟数组a0 = 64a1 = 34a2 = 25a3 = 12a4 = 22a5 = 11a6 = 90
print "Original array:"print a0, a1, a2, a3, a4, a5, a6
note: 简化的冒泡排序演示print "Note: Full array operations require extended features"print "See examples directory for more complex programs"更多示例
下载的源代码包中包含了 10 个完整的示例程序,位于 examples/ 目录:
01_hello.epp- Hello World02_variables.epp- 变量和数据类型03_conditionals.epp- 条件语句04_loops.epp- 循环结构05_functions.epp- 函数定义和使用06_calculator.epp- 简单计算器07_guess_number.epp- 猜数字游戏08_fibonacci.epp- 斐波那契数列09_bubble_sort.epp- 冒泡排序10_standard_library.epp- 标准库函数演示
下一步
现在你已经了解了 PureEnglish 的基础知识!尝试:
- 运行
examples/目录下的示例程序 - 修改示例程序,加入你自己的想法
- 创建全新的程序!
祝您编程愉快!