//
// main.swift
// Mhc
//
// Created by mhc team on 2023/1/12.
//
import Foundation
getbe()
switch(character)
{
case "a":
fallthrough
case "b":
fallthrough
case "c":
fallthrough
case "d":
fallthrough
case "e":
fallthrough
case "f":
fallthrough
case "g":
fallthrough
case "h":
fallthrough
case "i":
fallthrough
case "j":
fallthrough
case "k":
fallthrough
case "l":
fallthrough
case "m":
fallthrough
case "n":
fallthrough
case "o":
fallthrough
case "p":
fallthrough
case "q":
fallthrough
case "r":
fallthrough
case "s":
fallthrough
case "t":
fallthrough
case "u":
fallthrough
case "v":
fallthrough
case "w":
fallthrough
case "x":
fallthrough
case "y":
fallthrough
case "z":
while letter || digit {
concatenation()
character = readLine()!
}
retract()
var c = reserve()
if c == 0 {
buildlist()
print("id :",c)
} else {
print(c,": null")
}
break
case "0":
fallthrough
case "1":
fallthrough
case "2":
fallthrough
case "3":
fallthrough
case "4":
fallthrough
case "5":
fallthrough
case "6":
fallthrough
case "7":
fallthrough
case "8":
fallthrough
case "9":
while digit {
concatenation()
character = readLine()!
}
retract()
buildlist()
print("num :",endIndex)
break
case "+":
print("'+' : null")
break
case "-":
print("'-' : null")
break
case "*":
print("'*' : null")
break
case "/":
print("'/' : null")
break
case "<":
character = readLine()!
if(character=="=") {
print("relop : LE")
}
else {
retract()
print("relop : LT")
}
break
case "=":
character = readLine()!
if(character=="=") {
print("relop : EQ")
}
else {
retract()
print("'=' : null")
}
break
case ";":
print("';' : null")
break
default: error()
}
//
// common.swift
// Mhc
//
// Created by mhc team on 2023/1/12.
//
import Foundation
var token:[[Int:String]] = []
var endIndex = 0
var character = readLine()!
func error() -> Void {
print("illegal character: \(character)")
}
func getbe() -> Void {
while character == "" {
character = readLine()!
}
}
func retract() -> Void {
character = ""
}
var digitList: [Int] = []
var letterList: [Character] = []
var letter: Bool {
return character.count == 1 ? Character(character).isLetter : false
}
var digit: Bool {
return character.count == 1 ? Character(character).isHexDigit : false
}
func concatenation() -> Void {
endIndex = token.count - 1
token.append([endIndex+1:character])
}
func reserve() -> Int {
var keyword: String = ""
for i in 0 ... endIndex + 1{
guard let c = token[i][i] else {
return 0
}
keyword.append(c)
}
if keyword == "while" {
return 1
}
if keyword == "if" {
return 2
}
if keyword == "else" {
return 3
}
if keyword == "switch" {
return 4
}
if keyword == "case" {
return 5
}
return 0
}
func buildlist() -> Void {
if character.count == 1 {
let c = Character(character)
if c.isLetter {
letterList.append(c)
} else {
digitList.append(Int(character)!)
}
}
}