Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created May 18, 2024 08:28
Show Gist options
  • Save ashraf267/15801c38946fc2510240f51d03fe005b to your computer and use it in GitHub Desktop.
Save ashraf267/15801c38946fc2510240f51d03fe005b to your computer and use it in GitHub Desktop.
A CSC450 ass to build a program (in any lang) that recognizes var declaration in java
void main() {
// call func
// print(javaVarDecParser(
// accSpec: 'public',
// datatype: 'String',
// varName: 'uname',
// assOp: '=',
// val: 'ash267',
// sc: ';'));
print(javaVarDecParser(
accSpec: 'public', datatype: 'Money', varName: '76tree', sc: '+'));
}
enum ValidDatatypes { String, char, int, float, boolean }
enum AcceptedAccSpec { private, public, protected }
// var dec format
// [access specifier] datatype var_name = value;
// OR
// [access specifier] datatype var_name;
// e.g public int age = 6;
// 3 kinds access specifiers; public, private, and protected
bool javaVarDecParser(
{String accSpec = "",
required String datatype,
required String varName,
String assOp = "",
String val = "",
required String sc}) {
int testPassed = 0;
print('testPassed: $testPassed');
if (!accSpec.isEmpty) {
// contains a str; not empty
if (accSpec.contains(AcceptedAccSpec.private.name) ||
accSpec.contains(AcceptedAccSpec.public.name) ||
accSpec.contains(AcceptedAccSpec.protected.name)) {
// accepted access specifier found
// record!
testPassed = testPassed + 1;
print('check1, testPassed: $testPassed');
}
}
if (!assOp.isEmpty) {
// contains a str
if (assOp.contains('=')) {
// ass op found!
// record!
testPassed = testPassed + 1;
print('check2, testPassed: $testPassed');
}
}
if (!val.isEmpty) {
// record!
testPassed = testPassed + 1;
print('check3, testPassed: $testPassed');
}
if (datatype.contains(ValidDatatypes.String.name) ||
datatype.contains(ValidDatatypes.char.name) ||
datatype.contains(ValidDatatypes.int.name) ||
datatype.contains(ValidDatatypes.float.name) ||
datatype.contains(ValidDatatypes.boolean.name)) {
// valid datatype found
// record!
testPassed = testPassed + 1;
print('check4, testPassed: $testPassed');
}
if (!varName.contains(RegExp(r'[0-9]'), 0)) {
// var name doesn't start with a non-letter
// record!
testPassed = testPassed + 1;
print('check5, testPassed: $testPassed');
}
if (sc.contains(';')) {
// semicolon found
// record!
testPassed = testPassed + 1;
print('check6, testPassed: $testPassed');
}
// check test passed
if (testPassed < 3) {
print('failed! testPassed: $testPassed');
return false;
} else {
print('success! testPassed: $testPassed');
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment