Files
Interpreter/compiler/include/compiler/Lexer.h
T
Admin 26256a36b9 扩充内置功能块为 8 个:TON/TOF/TP/CTU/CTD/CTUD/R_TRIG/F_TRIG。
- isa:CAL_* 操作码连续占 24..31(CALL/RET 后移为 32/33,kOpCount=34);disasm 支持 5 个新操作码
- 词法/语法/链接:新 8 个关键字与冻结布局(TP=in/pt/q/et、CTD=cd/ld/pv/q/cv、
  CTUD=cu/cd/r/lu/pv/qu/qd/cv、R_TRIG/F_TRIG=clk/q)
- VM:do_cal 全 8 个语义(TP 脉冲、CTD 递减、CTUD 双向+装载、R_TRIG 上升沿、F_TRIG 下降沿);
  edge_prev_ 每槽 2 字节存边沿上次输入;exec_one 分发补 5 个新操作码
- 验证:TP 30ms 脉冲 2 周期、CTD 装载+递减到 q=1、CTUD cu/cd 双向、R/F_TRIG 交替;
  ctest 11/11 全绿
- 文档:指令与映像/词法/初步计划/符号表与链接/指令执行/扫描周期/使用说明 同步
2026-08-21 20:38:27 +08:00

97 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file Lexer.h
* @brief ST 子集词法分析
* @author
* @date 2026-08-21
*/
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace compiler {
// token 类型。关键字与 token 集见 Doc/compiler/词法.md。
enum class Tok {
// 关键字(25 个,与 12.1 冻结表一致)
PROGRAM,
FUNCTION,
FUNCTION_BLOCK,
END_PROGRAM,
END_FUNCTION,
END_FUNCTION_BLOCK,
VAR,
VAR_INPUT,
VAR_OUTPUT,
VAR_GLOBAL,
VAR_EXTERNAL,
END_VAR,
BOOL,
INT,
TIME,
IF,
ELSIF,
ELSE,
END_IF,
WHILE,
END_WHILE,
THEN, // 12.5 修订:IF/WHILE 语法需要,补入关键字表
DO, // 同上
AND,
OR,
NOT,
TRUE,
FALSE,
TON,
TOF,
TP,
CTU,
CTD,
CTUD,
R_TRIG,
F_TRIG,
// 字面量
IDENT, // text = 小写名
INT_LIT, // int_value
TIME_LIT, // int_value = 毫秒
// 符号
ASSIGN, // :=
EQ, // =
NE, // <>
LT, // <
LE, // <=
GT, // >
GE, // >=
PLUS, // +
MINUS, // -
STAR, // *
SLASH, // /
LPAREN, // (
RPAREN, // )
COMMA, // ,
SEMI, // ;
COLON, // :
DOT, // .
END, // 文件结束
};
struct Token {
Tok type;
std::string text; // 标识符/关键字的小写文本;字面量原文
int64_t int_value; // INT_LIT / TIME_LIT(毫秒)
std::string source_file;
uint32_t line; // 从 1 起
uint32_t col; // 从 1 起
};
// 对源文本 lex 成 token 流(含末尾 END)。失败返回 false,
// err 前缀 "lex error",带文件与行列。
bool lex(const std::string& source_file, const std::string& content,
std::vector<Token>* out, std::string* err);
// 读文件后 lex(文件不存在 / 读取失败也算 lex error
bool lex_file(const std::string& path, std::vector<Token>* out, std::string* err);
}