├── .gitignore ├── AndroidManifest.xml ├── README ├── build.properties.SAMPLE ├── build.xml ├── default.properties ├── go.sh ├── go_install.sh ├── go_install_emu.sh ├── libs └── admob-sdk-android.jar ├── local.properties.SAMPLE ├── res ├── drawable-hdpi │ ├── alert_dialog_icon.png │ └── icon.png ├── drawable-ldpi │ └── icon.png ├── drawable-mdpi │ ├── alert_dialog_icon.png │ └── icon.png ├── layout │ ├── dialog_filename.xml │ ├── dialog_input.xml │ └── main.xml ├── values-ru │ └── strings.xml └── values │ ├── attrs.xml │ └── strings.xml └── src ├── me └── zed_0xff │ └── android │ └── pascal │ └── Main.java └── net └── sourceforge └── biff ├── Codes.java └── Scanner.java /.gitignore: -------------------------------------------------------------------------------- 1 | bin/* 2 | gen/* 3 | *.keystore 4 | build.properties 5 | local.properties 6 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a Pascal language interpreter for Android platform. 2 | 3 | It uses a Pascal interpreter written in Java - http://sourceforge.net/projects/biff/ - by Lukasz F. 4 | 5 | Please fork/commit/pull if you want to improve it. 6 | 7 | How to build: 8 | 1. install Android SDK from http://developer.android.com/sdk/ 9 | 2. set up path to SDK in local.properties file 10 | 3. [optionally] generate your private/public keys & set it up in build.properties file 11 | 4. issue "ant debug" or "ant release" command 12 | 5. pick up "Pascal-debug.apk" or "Pascal-release.apk" from "bin" subdirectory 13 | 6. enjoy! :) 14 | -------------------------------------------------------------------------------- /build.properties.SAMPLE: -------------------------------------------------------------------------------- 1 | # This file is used to override default values used by the Ant build system. 2 | # 3 | # This file must be checked in Version Control Systems, as it is 4 | # integral to the build system of your project. 5 | 6 | # This file is only used by the Ant script. 7 | 8 | # You can use this to override default values such as 9 | # 'source.dir' for the location of your java source folder and 10 | # 'out.dir' for the location of your output folder. 11 | 12 | # You can also use it define how the release builds are signed by declaring 13 | # the following properties: 14 | # 'key.store' for the location of your keystore and 15 | # 'key.alias' for the name of the key to use. 16 | # The password will be asked during the build when you use the 'release' target. 17 | key.store=release.keystore 18 | key.alias=release 19 | key.store.password=mypassword 20 | key.alias.password=mypassword 21 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 29 | 30 | 31 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 50 | 51 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /default.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "build.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Project target. 11 | target=android-7 12 | -------------------------------------------------------------------------------- /go.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$1" = "" ]; then 4 | DST=5554 5 | else 6 | DST=$1 7 | fi 8 | 9 | #ant debug && adb -s emulator-$DST install -r bin/*-debug.apk 10 | ant release && scp bin/Pascal-release.apk orbitel.ru:www/p.apk 11 | -------------------------------------------------------------------------------- /go_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$1" = "" ]; then 4 | DST=5554 5 | else 6 | DST=$1 7 | fi 8 | 9 | #ant debug && adb -s emulator-$DST install -r bin/*-debug.apk 10 | ant release && adb install -r bin/*-release.apk 11 | -------------------------------------------------------------------------------- /go_install_emu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$1" = "" ]; then 4 | DST=5554 5 | else 6 | DST=$1 7 | fi 8 | 9 | ant release && adb -s emulator-$DST install -r bin/*-release.apk 10 | #ant release && adb install -r bin/*-release.apk 11 | -------------------------------------------------------------------------------- /libs/admob-sdk-android.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/libs/admob-sdk-android.jar -------------------------------------------------------------------------------- /local.properties.SAMPLE: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked in Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | 7 | # location of the SDK. This is only used by Ant 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | sdk.dir=/path/to/android_sdk 11 | -------------------------------------------------------------------------------- /res/drawable-hdpi/alert_dialog_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/res/drawable-hdpi/alert_dialog_icon.png -------------------------------------------------------------------------------- /res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /res/drawable-mdpi/alert_dialog_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/res/drawable-mdpi/alert_dialog_icon.png -------------------------------------------------------------------------------- /res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zed-0xff/android-pascal/7188a76f671adb650a329a9df4602733e641801d/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /res/layout/dialog_filename.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /res/layout/dialog_input.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 18 | 19 | 24 | 25 | 28 | 29 | 34 | 35 | 39 | 40 | 41 | 42 | 43 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /res/values-ru/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Паскаль 4 | Файл 5 | Код 6 | Запуск 7 | Новая программа 8 | Сохранить 9 | сохранен 10 | Имя файла 11 | Отмена 12 | Удалить 13 | Ввод 14 | 15 | -------------------------------------------------------------------------------- /res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pascal 4 | File 5 | Edit 6 | Run 7 | New Program 8 | Save 9 | saved 10 | Enter filename 11 | Cancel 12 | Delete 13 | Input 14 | 15 | -------------------------------------------------------------------------------- /src/me/zed_0xff/android/pascal/Main.java: -------------------------------------------------------------------------------- 1 | // vim:ts=4:sw=4:expandtab 2 | package me.zed_0xff.android.pascal; 3 | 4 | import android.app.TabActivity; 5 | import android.app.Activity; 6 | import android.os.Bundle; 7 | import android.widget.TabHost; 8 | import android.widget.TabHost.OnTabChangeListener; 9 | import android.widget.TabHost.TabSpec; 10 | import android.widget.TextView; 11 | import android.widget.EditText; 12 | import android.widget.ListView; 13 | import android.widget.ArrayAdapter; 14 | import android.view.LayoutInflater; 15 | import android.view.View; 16 | import android.view.View.OnKeyListener; 17 | import android.widget.AdapterView.OnItemClickListener; 18 | import android.widget.AdapterView; 19 | import android.view.KeyEvent; 20 | import android.view.Menu; 21 | import android.view.MenuItem; 22 | import android.view.ContextMenu; 23 | import android.view.ContextMenu.*; 24 | import android.view.View.OnCreateContextMenuListener; 25 | import android.app.AlertDialog; 26 | import android.content.DialogInterface; 27 | import android.app.Dialog; 28 | import android.widget.Toast; 29 | import android.widget.AdapterView.AdapterContextMenuInfo; 30 | 31 | import java.util.Date; 32 | import java.text.DateFormat; 33 | import java.text.SimpleDateFormat; 34 | import java.util.ArrayList; 35 | import java.util.Arrays; 36 | 37 | import java.io.*; 38 | 39 | import net.sourceforge.biff.Scanner; 40 | 41 | import me.zed_0xff.android.pascal.R; 42 | 43 | import com.admob.android.ads.AdManager; 44 | 45 | public class Main extends Activity { 46 | 47 | private ArrayAdapter mAdapter; 48 | private ArrayList mStrings = new ArrayList(); 49 | 50 | private String currentFilename, currentText, inputtedText; 51 | 52 | private static final int MENU_NEW = Menu.FIRST; 53 | private static final int MENU_SAVE = Menu.FIRST+1; 54 | private static final int MENU_RENAME = Menu.FIRST+2; 55 | private static final int MENU_DELETE = Menu.FIRST+3; 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | 61 | //AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } ); 62 | 63 | setContentView(R.layout.main); 64 | 65 | TabHost tabHost = (TabHost) this.findViewById(R.id.my_tabhost); 66 | tabHost.setup(); 67 | 68 | ListView files_view = (ListView) this.findViewById(R.id.files); 69 | files_view.setOnItemClickListener(new OnItemClickListener(){ 70 | public void onItemClick(AdapterView parent, View view, int position, long id){ 71 | String fn = (String)parent.getItemAtPosition(position); 72 | load_file(fn); 73 | } 74 | }); 75 | files_view.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ 76 | public void onCreateContextMenu(ContextMenu menu, View v, 77 | ContextMenuInfo menuInfo) { 78 | //super.onCreateContextMenu(menu, v, menuInfo); 79 | //menu.add(0, MENU_RENAME, 0, "Rename"); 80 | menu.add(0, MENU_DELETE, 0, getResources().getText(R.string.delete)); 81 | } 82 | }); 83 | 84 | mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mStrings); 85 | files_view.setAdapter( mAdapter ); 86 | 87 | tabHost.addTab(tabHost.newTabSpec("files") 88 | .setIndicator(getResources().getText(R.string.file)) 89 | .setContent(R.id.files)); 90 | tabHost.addTab(tabHost.newTabSpec("edit") 91 | .setIndicator(getResources().getText(R.string.edit)) 92 | .setContent(R.id.edit)); 93 | tabHost.addTab(tabHost.newTabSpec("run") 94 | .setIndicator(getResources().getText(R.string.run)) 95 | .setContent(R.id.run)); 96 | 97 | tabHost.setCurrentTabByTag("edit"); 98 | 99 | tabHost.setOnTabChangedListener(new OnTabChangeListener(){ 100 | @Override 101 | public void onTabChanged(String tabId) { 102 | android.view.View adView = findViewById( R.id.ad ); 103 | 104 | if( tabId == "run" ) { 105 | adView.setVisibility(android.view.View.VISIBLE); 106 | runProgram(); 107 | } else if( tabId == "files" ) { 108 | adView.setVisibility(android.view.View.VISIBLE); 109 | updateFilelist(); 110 | } else { 111 | // edit 112 | adView.setVisibility(android.view.View.GONE); 113 | } 114 | } 115 | }); 116 | } 117 | 118 | void updateFilelist(){ 119 | String[] fnames = fileList(); 120 | Arrays.sort(fnames); 121 | mAdapter.clear(); 122 | for( String fname : fnames ) mAdapter.add( fname ); 123 | } 124 | 125 | void runProgram(){ 126 | TextView code= (TextView) findViewById( R.id.edit ); 127 | TextView run = (TextView) findViewById( R.id.run ); 128 | String stdout = "", stderr = ""; 129 | try { 130 | Scanner scanner = new Scanner(code.getText().toString(), this); 131 | stderr = scanner.stderr; 132 | stdout = scanner.stdout; 133 | } catch( Exception e ) { 134 | stderr = "EXCEPTION!\n" + e.toString() + "\n" + e.getStackTrace()[0].toString(); 135 | } 136 | 137 | run.setText(stdout); 138 | 139 | if( stderr.length() != 0 ){ 140 | run.append("\n" + stderr); 141 | } 142 | } 143 | 144 | public boolean onCreateOptionsMenu(Menu menu) { 145 | menu.add(0, MENU_NEW, 0, getResources().getText(R.string.new_program)). 146 | setIcon(android.R.drawable.ic_menu_add); 147 | menu.add(0, MENU_SAVE, 0, getResources().getText(R.string.save)). 148 | setIcon(android.R.drawable.ic_menu_save); 149 | return true; 150 | } 151 | 152 | public boolean onOptionsItemSelected(MenuItem item) { 153 | switch (item.getItemId()) { 154 | case MENU_NEW: 155 | newProgram(); 156 | return true; 157 | case MENU_SAVE: 158 | save(); 159 | return true; 160 | } 161 | return false; 162 | } 163 | 164 | public boolean onContextItemSelected(MenuItem item) { 165 | AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 166 | switch (item.getItemId()) { 167 | case MENU_RENAME: 168 | return true; 169 | case MENU_DELETE: 170 | ListView files_view = (ListView) this.findViewById(R.id.files); 171 | String fn = (String)files_view.getItemAtPosition(info.position); 172 | deleteFile(fn); 173 | updateFilelist(); 174 | return true; 175 | default: 176 | return super.onContextItemSelected(item); 177 | } 178 | } 179 | 180 | private static final int FILENAME_DIALOG = 0; 181 | private static final int INPUT_DIALOG = 1; 182 | 183 | protected Dialog onCreateDialog(int id) { 184 | LayoutInflater factory = LayoutInflater.from(this); 185 | 186 | if( id == FILENAME_DIALOG ){ 187 | final View filenameEntryView = factory.inflate(R.layout.dialog_filename, null); 188 | 189 | return new AlertDialog.Builder(this) 190 | .setIcon(R.drawable.alert_dialog_icon) 191 | .setTitle(getResources().getText(R.string.filename)) 192 | .setView(filenameEntryView) 193 | .setPositiveButton(getResources().getText(R.string.save), new DialogInterface.OnClickListener() { 194 | public void onClick(DialogInterface dialog, int whichButton) { 195 | /* User clicked OK */ 196 | TextView t = (TextView) ((Dialog)dialog).findViewById(R.id.filename_edit); 197 | currentFilename = t.getText().toString(); 198 | save_current_file(); 199 | } 200 | }) 201 | .setNegativeButton(getResources().getText(R.string.cancel), new DialogInterface.OnClickListener() { 202 | public void onClick(DialogInterface dialog, int whichButton) { 203 | /* User clicked cancel */ 204 | } 205 | }) 206 | .create(); 207 | } else { 208 | final View inputView = factory.inflate(R.layout.dialog_input, null); 209 | 210 | return new AlertDialog.Builder(this) 211 | .setIcon(R.drawable.alert_dialog_icon) 212 | .setTitle(getResources().getText(R.string.input)) 213 | .setView(inputView) 214 | .setPositiveButton("OK", new DialogInterface.OnClickListener() { 215 | public void onClick(DialogInterface dialog, int whichButton) { 216 | /* User clicked OK */ 217 | TextView t = (TextView) ((Dialog)dialog).findViewById(R.id.input_edit); 218 | inputtedText = t.getText().toString(); 219 | } 220 | }) 221 | .create(); 222 | } 223 | } 224 | 225 | protected void onPrepareDialog(int id, Dialog d) { 226 | EditText t = (EditText) d.findViewById(R.id.filename_edit); 227 | 228 | if(currentFilename == null || currentFilename == ""){ 229 | t.setText(guessProgramName()); 230 | } else { 231 | t.setText(currentFilename); 232 | } 233 | t.selectAll(); 234 | } 235 | 236 | String generateProgramName() { 237 | DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm"); 238 | Date date = new Date(); 239 | return "p" + dateFormat.format(date); 240 | } 241 | 242 | void newProgram(){ 243 | currentFilename = null; 244 | 245 | TabHost tabHost = (TabHost) this.findViewById(R.id.my_tabhost); 246 | tabHost.setCurrentTabByTag("edit"); 247 | 248 | EditText t = (EditText) this.findViewById(R.id.edit); 249 | t.setText("program "+generateProgramName()+";\nbegin\n \nend."); 250 | t.setSelection(31,31); 251 | } 252 | 253 | String guessProgramName(){ 254 | TextView t = (TextView) this.findViewById(R.id.edit); 255 | String text = t.getText().toString(); 256 | System.out.println(text); 257 | int pos = text.indexOf("program "); 258 | if(pos > -1 ){ 259 | pos += 8; 260 | int len=0; 261 | char c = text.charAt(pos+len); 262 | while( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ){ 263 | len++; 264 | c = text.charAt(pos+len); 265 | } 266 | if( len > 0 ){ 267 | return text.substring(pos,pos+len); 268 | } 269 | } 270 | return generateProgramName(); 271 | } 272 | 273 | void load_file( String fn ){ 274 | currentFilename = null; 275 | try{ 276 | FileInputStream f = openFileInput(fn); 277 | byte[] buffer = new byte[f.available()]; 278 | f.read(buffer); 279 | f.close(); 280 | String text = new String(buffer); 281 | TextView t = (TextView) this.findViewById(R.id.edit); 282 | t.setText(text); 283 | TabHost tabHost = (TabHost) this.findViewById(R.id.my_tabhost); 284 | tabHost.setCurrentTabByTag("edit"); 285 | Toast.makeText(Main.this, fn + " loaded", Toast.LENGTH_SHORT).show(); 286 | currentFilename = fn; 287 | } catch(IOException ex) { 288 | Toast.makeText(Main.this, "Error loading file!", Toast.LENGTH_SHORT).show(); 289 | } 290 | } 291 | 292 | void save_current_file(){ 293 | if(currentFilename != null && currentFilename != ""){ 294 | String fn = currentFilename; 295 | if(!fn.endsWith(".pas")) fn += ".pas"; 296 | try{ 297 | TextView t = (TextView) this.findViewById(R.id.edit); 298 | FileOutputStream f = openFileOutput(fn, MODE_WORLD_READABLE); 299 | f.write(t.getText().toString().getBytes()); 300 | f.close(); 301 | Toast.makeText(Main.this, fn + " " + getResources().getText(R.string.saved), Toast.LENGTH_SHORT).show(); 302 | } catch(IOException ex) { 303 | Toast.makeText(Main.this, "Error saving file!", Toast.LENGTH_SHORT).show(); 304 | } 305 | } 306 | } 307 | 308 | void save(){ 309 | if(currentFilename == null || currentFilename == ""){ 310 | showDialog(FILENAME_DIALOG); 311 | } else { 312 | save_current_file(); 313 | } 314 | } 315 | 316 | public String inputText(){ 317 | showDialog(INPUT_DIALOG); 318 | return inputtedText; 319 | } 320 | } 321 | 322 | -------------------------------------------------------------------------------- /src/net/sourceforge/biff/Codes.java: -------------------------------------------------------------------------------- 1 | /*"Pascal interpreter written in Java" jest przeznaczony do 2 | interpretacji kodu napisanego w języku Pascal. 3 | Copyright (C) 2004/2005 Bartyna Waldemar, Faderewski Marek, 4 | Fedorczyk Łukasz, Iwanowski Wojciech. 5 | Niniejszy program jest wolnym oprogramowaniem; możesz go 6 | rozprowadzać dalej i/lub modyfikować na warunkach Powszechnej 7 | Licencji Publicznej GNU, wydanej przez Fundację Wolnego 8 | Oprogramowania - według wersji 2-giej tej Licencji lub którejś 9 | z późniejszych wersji. 10 | 11 | Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on 12 | użyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domyślnej 13 | gwarancji PRZYDATNOŚCI HANDLOWEJ albo PRZYDATNOŚCI DO OKREŚLONYCH 14 | ZASTOSOWAŃ. W celu uzyskania bliższych informacji - Powszechna 15 | Licencja Publiczna GNU. 16 | 17 | Z pewnością wraz z niniejszym programem otrzymałeś też egzemplarz 18 | Powszechnej Licencji Publicznej GNU (GNU General Public License); 19 | jeśli nie - napisz do Free Software Foundation, Inc., 675 Mass Ave, 20 | Cambridge, MA 02139, USA. 21 | */ 22 | 23 | package net.sourceforge.biff; 24 | 25 | import java.io.*; 26 | import java.util.ArrayList; 27 | 28 | public class Codes { 29 | 30 | // codes for keywords 31 | static final byte PROGRAM = 10; 32 | static final byte BEGIN = 11; 33 | static final byte END = 12; 34 | static final byte END_DOT = 13; // END. 35 | static final byte VAR = 14; 36 | static final byte IF = 15; 37 | static final byte THEN = 16; 38 | static final byte ELSE = 17; 39 | static final byte WHILE = 18; 40 | static final byte DO = 19; 41 | static final byte NOT = 20; 42 | static final byte AND = 21; 43 | static final byte OR = 22; 44 | static final byte READLN = 23; 45 | static final byte WRITELN = 24; 46 | static final byte INTEGER = 25; 47 | static final byte REAL = 26; 48 | static final byte BOOLEAN = 27; 49 | static final byte TRUE = 28; 50 | static final byte FALSE = 29; 51 | 52 | // codes for signs 53 | static final byte PLUS = 30; // '+' 54 | static final byte MINUS = 31; // '-' 55 | static final byte MULT = 32; // '*' 56 | static final byte DIV = 33; // '/' 57 | static final byte E = 34; // EQUAL 58 | static final byte NE = 35; // NOTEQUAL 59 | static final byte GT = 36; // GREATER THEN 60 | static final byte GE = 37; // GREATER OR EQUAL 61 | static final byte LT = 38; // LESS THEN 62 | static final byte LE = 39; // LESS OR EQUAL 63 | static final byte COLON = 40; // ':' 64 | static final byte SEMICOLON = 41; // ';' 65 | static final byte COMMA = 42; // ',' 66 | static final byte ASOCIATE = 43; // ':=' 67 | static final byte LBRACKET = 44; // LEFT BRACKET 68 | static final byte RBRACKET = 45; // RIGHT BRACKET 69 | static final byte QUOTATION = 46; // QUOTATION MARK 70 | 71 | // codes for lower categories 72 | static final byte KEYWORD = 60; 73 | static final byte VARIABLE = 61; 74 | static final byte VTYPE = 62; // VARIABLE TYPE 75 | static final byte LOPERATOR = 63; // LOGICAL OPERATOR 76 | static final byte AOPERATOR = 64; // ARITHMETIC OPERATOR 77 | static final byte ROPERATOR = 65; // RELATIONAL OPERATOR 78 | static final byte OOPERATOR = 66; // OTHER OPERATORS 79 | static final byte NUMBER = 67; 80 | 81 | // codes for higher categories 82 | static final byte INUMBER = 1; // INTEGER NUMBER 83 | static final byte RNUMBER = 2; // REAL NUMBER 84 | static final byte WORD = 3; 85 | static final byte SIGN = 4; 86 | static final byte STRING = 5; 87 | 88 | public ArrayList keywords = new ArrayList();; 89 | private ArrayList signs = new ArrayList();; 90 | 91 | public void fillLists() { 92 | keywords.add("PROGRAM"); 93 | keywords.add("BEGIN"); 94 | keywords.add("END"); 95 | keywords.add("END."); 96 | keywords.add("VAR"); 97 | keywords.add("IF"); 98 | keywords.add("THEN"); 99 | keywords.add("ELSE"); 100 | keywords.add("WHILE"); 101 | keywords.add("DO"); 102 | keywords.add("NOT"); 103 | keywords.add("AND"); 104 | keywords.add("OR"); 105 | keywords.add("READLN"); 106 | keywords.add("WRITELN"); 107 | keywords.add("INTEGER"); 108 | keywords.add("REAL"); 109 | keywords.add("BOOLEAN"); 110 | keywords.add("TRUE"); 111 | keywords.add("FALSE"); 112 | 113 | signs.add("+"); 114 | signs.add("-"); 115 | signs.add("*"); 116 | signs.add("/"); 117 | signs.add("="); 118 | signs.add("<>"); 119 | signs.add(">"); 120 | signs.add(">="); 121 | signs.add("<"); 122 | signs.add("<="); 123 | signs.add(":"); 124 | signs.add(";"); 125 | signs.add(","); 126 | signs.add(":="); 127 | signs.add("("); 128 | signs.add(")"); 129 | signs.add("\""); 130 | }// fillLists() 131 | 132 | public byte getKeywordCode(String value) { 133 | int index = keywords.indexOf(value); 134 | if (index == -1) 135 | return VARIABLE; 136 | else 137 | return (byte)(10 + index); 138 | }// getKeywordCode(Stirng) 139 | 140 | public byte getSignCode(String value) { 141 | return (byte)(30 + signs.indexOf(value)); 142 | }// getSignCode(Stirng) 143 | } 144 | -------------------------------------------------------------------------------- /src/net/sourceforge/biff/Scanner.java: -------------------------------------------------------------------------------- 1 | /*"Pascal interpreter written in Java" jest przeznaczony do 2 | interpretacji kodu napisanego w języku Pascal. 3 | Copyright (C) 2004/2005 Bartyna Waldemar, Faderewski Marek, 4 | Fedorczyk Łukasz, Iwanowski Wojciech. 5 | Niniejszy program jest wolnym oprogramowaniem; możesz go 6 | rozprowadzać dalej i/lub modyfikować na warunkach Powszechnej 7 | Licencji Publicznej GNU, wydanej przez Fundację Wolnego 8 | Oprogramowania - według wersji 2-giej tej Licencji lub którejś 9 | z późniejszych wersji. 10 | 11 | Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on 12 | użyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domyślnej 13 | gwarancji PRZYDATNOŚCI HANDLOWEJ albo PRZYDATNOŚCI DO OKREŚLONYCH 14 | ZASTOSOWAŃ. W celu uzyskania bliższych informacji - Powszechna 15 | Licencja Publiczna GNU. 16 | 17 | Z pewnością wraz z niniejszym programem otrzymałeś też egzemplarz 18 | Powszechnej Licencji Publicznej GNU (GNU General Public License); 19 | jeśli nie - napisz do Free Software Foundation, Inc., 675 Mass Ave, 20 | Cambridge, MA 02139, USA. 21 | */ 22 | 23 | package net.sourceforge.biff; 24 | 25 | import java.io.*; 26 | import java.util.ArrayList; 27 | 28 | import me.zed_0xff.android.pascal.*; 29 | 30 | public class Scanner { 31 | 32 | public ArrayList lexems = new ArrayList(); 33 | 34 | // variables for validation process 35 | ArrayList vars = new ArrayList(); 36 | ArrayList var_types = new ArrayList(); 37 | ArrayList var_vals = new ArrayList(); 38 | // position in Advenced Lexems Array 39 | private int pos = 0; 40 | // position during instruction evaluating 41 | private int place = 0; 42 | private int main_begin = 0; 43 | 44 | public String stderr; 45 | public String stdout; 46 | 47 | private Main activity; 48 | 49 | public Scanner(String data, Main activity_) throws Exception { 50 | 51 | stderr = ""; 52 | stdout = ""; 53 | activity = activity_; 54 | 55 | StringReader filein = new StringReader(data); 56 | 57 | // array of 'lexem' objects 58 | lexems = new ArrayList(); 59 | 60 | // current lexem 61 | StringBuffer value = new StringBuffer(); 62 | 63 | // current type of lexem 64 | byte type = 0; 65 | 66 | // current line 67 | int line = 1; 68 | 69 | // current column 70 | int column = 1; 71 | 72 | // where the lexem began 73 | int began = 1; 74 | 75 | // where the String began 76 | int sline = 1; 77 | 78 | int bite = filein.read(); 79 | while (bite != -1) { 80 | // if bite is a quotation mark 81 | if (bite == 34) { 82 | if (type != 0) { 83 | lexems.add(new Lexem(value, type, line, began)); 84 | value = new StringBuffer(); 85 | } 86 | if (type != 5) { 87 | type = 5; 88 | began = column; 89 | sline = line; 90 | } 91 | else 92 | type = 0; 93 | } 94 | else if (type == 5) { 95 | value.append((char)bite); 96 | if (bite == 13) { 97 | line ++; 98 | column = -1; 99 | } 100 | } 101 | // if bite is a digit 102 | else if ((bite >= 48) && (bite <= 57)) { 103 | if (type == 0) { 104 | type = 1; 105 | began = column; 106 | } 107 | value.append((char)bite); 108 | } 109 | else 110 | // if bite is a letter 111 | if (((bite >= 65) && (bite <= 90)) || 112 | ((bite >= 97) && (bite <= 122))) { 113 | if (type == 0) { 114 | type = 3; 115 | began = column; 116 | value.append((char)bite); 117 | } 118 | else 119 | if (type == 3) 120 | value.append((char)bite); 121 | else { 122 | type = 7; 123 | break; 124 | } 125 | } 126 | else 127 | // if bite is a sign 128 | if (((bite >= 40) && (bite <= 45)) || 129 | (bite == 47) || 130 | ((bite >= 58) && (bite <= 62))) { 131 | if (type != 0) { 132 | lexems.add(new Lexem(value, type, line, began)); 133 | type = 0; 134 | value = new StringBuffer(); 135 | } 136 | lexems.add(new Lexem(bite, 4, line, column)); 137 | } 138 | else 139 | // if bite is a dot 140 | if (bite == 46) 141 | if (type != 0) 142 | if (type == 3) 143 | if (((value.toString()) 144 | .toUpperCase()).equals("END")) { 145 | lexems.add(new Lexem("END.", 146 | (byte)3, line, began)); 147 | type = 0; 148 | value = new StringBuffer(); 149 | } 150 | else { 151 | type = 8; 152 | break; 153 | } 154 | else 155 | if (type == 1) { 156 | type = 2; 157 | value.append('.'); 158 | } 159 | else { 160 | type = 8; 161 | break; 162 | } 163 | else { 164 | type = 8; 165 | break; 166 | } 167 | else 168 | // if bite is a separator 169 | if ((bite == 9) || (bite == 32) || 170 | (bite == 13) || (bite == 10)) { 171 | if (type != 0) { 172 | lexems.add(new Lexem(value, 173 | type, line, began)); 174 | type = 0; 175 | value = new StringBuffer(); 176 | } 177 | if (bite == 13) { 178 | line ++; 179 | column = -1; 180 | } 181 | } 182 | // bite is not allowed character 183 | else { 184 | type = 9; 185 | break; 186 | } 187 | bite = filein.read(); 188 | if ((bite == -1) && (type == 5)) 189 | type = 6; 190 | else if (bite == -1) 191 | if (type != 0) { 192 | lexems.add(new Lexem(value, type, line, began)); 193 | type = 0; 194 | value = new StringBuffer(); 195 | } 196 | column ++; 197 | } 198 | 199 | // error handling 200 | if (type == 6) 201 | err("ERROR: String not closed ! " 202 | + "line: " + sline + ", column: " + began); 203 | else if (type == 7) 204 | err("ERROR: letter joined with number ! " 205 | + "line: " + line + ", column: " + column); 206 | else 207 | if (type == 8) 208 | err("ERROR: dot in wrong place ! " 209 | + "line: " + line + ", column: " + column); 210 | else 211 | if (type == 9) 212 | err("ERROR: illegal character !: " 213 | + (char)bite 214 | + " line: " + line + ", column: " + column); 215 | else { 216 | // if everything is OK 217 | // printLexems(lexems); 218 | // advenced scunning (father, deeper clasification) 219 | ArrayList AdvL = advScanning(lexems); 220 | // printAdvLexems(AdvL); 221 | // validation process 222 | String tmp_result = checkStructure(AdvL); 223 | if (tmp_result.equals("No errors !")) { 224 | //System.out.println("==================="); 225 | // variables initialization 226 | initializeVars(); 227 | pos = main_begin; 228 | // interpretation process 229 | ArrayList ExecL = interBody(AdvL); 230 | // execution 231 | execute(ExecL); 232 | 233 | 234 | /* 235 | System.out.println("IAL Size = " + ExecL.size()); 236 | for (int i = 0; i < ExecL.size(); i++) { 237 | System.out.println(((ExecLexem)ExecL.get(i)).type); 238 | System.out.println(((ExecLexem)ExecL.get(i)).variable); 239 | System.out.println("If TRUE SIZE = " + ((ExecLexem)ExecL.get(i)). 240 | iftrue.size()); 241 | for (int j = 0; j < ((ExecLexem)ExecL.get(i)).lexems.length; j++) 242 | System.out.println(((ExecLexem)ExecL.get(i)).lexems[j].value); 243 | } */ 244 | 245 | } else { 246 | err("ERROR!"); 247 | err(tmp_result); 248 | } 249 | } 250 | }// Scanner() 251 | 252 | public void printLexems(ArrayList lex) { 253 | System.out.println("\nLEXEMS: " + lex.size()); 254 | System.out.println("VALUE TYPE LINE COLUMN"); 255 | Object[] lexems = lex.toArray(); 256 | for (int i = 0; i < lexems.length; i ++) { 257 | Lexem lexem = (Lexem)lexems[i]; 258 | System.out.println(lexem.value + " " + lexem.type + " " 259 | + lexem.line + " " + lexem.column); 260 | } 261 | System.out.println(); 262 | }// printLexems(ArrayList) 263 | 264 | public void printAdvLexems(ArrayList AdvLex) { 265 | System.out.println("\nLEXEMS: " + AdvLex.size()); 266 | System.out.println("VALUE CODE LCATEGORY " 267 | + "HCATEGORY LINE COLUMN"); 268 | Object[] AdvLexems = AdvLex.toArray(); 269 | for (int i = 0; i < AdvLexems.length; i ++) { 270 | AdvLexem advlexem = (AdvLexem)AdvLexems[i]; 271 | System.out.println(advlexem.value + " " + advlexem.code + " " 272 | + advlexem.lcategory + " " 273 | + advlexem.hcategory + " " 274 | + advlexem.line + " " + advlexem.column); 275 | } 276 | System.out.println(); 277 | }// printLexems(ArrayList) 278 | 279 | public ArrayList advScanning(ArrayList lexems) { 280 | ArrayList AdvLexems = new ArrayList(); 281 | String value = ""; 282 | Codes codes = new Codes(); 283 | codes.fillLists(); 284 | Lexem lexem = null; 285 | byte code = 0; 286 | byte lcategory = 0; 287 | byte hcategory = 0; 288 | 289 | int size = lexems.size(); 290 | int i = 0; 291 | while (i < size) { 292 | lexem = (Lexem)lexems.get(i); 293 | 294 | // if lexem is a word 295 | if (lexem.type == codes.STRING) 296 | AdvLexems.add(new AdvLexem(lexem.value, codes.STRING, 297 | codes.STRING, codes.STRING, 298 | lexem.line, lexem.column)); 299 | else if (lexem.type == codes.WORD) { 300 | hcategory = codes.WORD; 301 | code = codes.getKeywordCode(lexem.value); 302 | 303 | if (code == codes.VARIABLE) 304 | lcategory = codes.VARIABLE; 305 | else if ((code >= codes.AND) && (code <= codes.OR)) 306 | lcategory = codes.LOPERATOR; 307 | else if ((code >= codes.INTEGER) && (code <= codes.BOOLEAN)) 308 | lcategory = codes.VTYPE; 309 | else 310 | lcategory = codes.KEYWORD; 311 | 312 | AdvLexems.add(new AdvLexem(lexem.value, code, 313 | lcategory, hcategory, 314 | lexem.line, lexem.column)); 315 | } 316 | 317 | // if lexem is a sign 318 | else if (lexem.type == codes.SIGN) { 319 | value = lexem.value;; 320 | hcategory = codes.SIGN; 321 | code = codes.getSignCode(lexem.value); 322 | 323 | if ((code >= codes.COLON) && (code <= codes.QUOTATION)) { 324 | lcategory = codes.OOPERATOR; 325 | if (lexem.value.equals(":")) 326 | if ((i + 1) < size) 327 | if (((Lexem)lexems.get(i + 1)).value.equals("=")) { 328 | code = codes.ASOCIATE; 329 | value = ":="; 330 | i ++; 331 | } 332 | } 333 | else if ((code >= codes.E) && (code <= codes.LE)) { 334 | lcategory = codes.ROPERATOR; 335 | if (lexem.value.equals("<")) 336 | if ((i + 1) < size) { 337 | if (((Lexem)lexems.get(i + 1)).value.equals("=")) { 338 | code = codes.LE; 339 | value = "<="; 340 | i ++; 341 | } 342 | if (((Lexem)lexems.get(i + 1)).value.equals(">")) { 343 | code = codes.NE; 344 | value = "<>"; 345 | i ++; 346 | } 347 | } 348 | else if (lexem.value.equals(">")) 349 | if ((i + 1) < size) 350 | if (((Lexem)lexems.get(i + 1)).value.equals("=")) { 351 | code = codes.GE; 352 | value = ">="; 353 | i ++; 354 | } 355 | } 356 | else if ((code >= codes.PLUS) && (code <= codes.DIV)) { 357 | lcategory = codes.AOPERATOR; 358 | } 359 | AdvLexems.add(new AdvLexem(value, code, 360 | lcategory, hcategory, 361 | lexem.line, lexem.column)); 362 | } 363 | else 364 | AdvLexems.add(new AdvLexem(lexem.value, lexem.type, 365 | codes.NUMBER, codes.WORD, 366 | lexem.line, lexem.column)); 367 | i ++; 368 | } 369 | return AdvLexems; 370 | }// advScanning() 371 | 372 | private String errorMess(String expected, AdvLexem found) { 373 | return ("expected: " + expected 374 | + "\n found: " + found.value 375 | + "\n line: " + found.line 376 | + "\n column: " + found.column); 377 | }// errorMess() 378 | 379 | private String errorMess(String expected) { 380 | return ("expected: " + expected 381 | + " nothing found"); 382 | }// errorMess() 383 | 384 | 385 | // checks if there is a lexem of given Code 386 | private String checkC(String s, byte code, ArrayList AL) { 387 | if (pos < AL.size()) { 388 | AdvLexem al = (AdvLexem)AL.get(pos); 389 | if (al.code != code) 390 | return errorMess(s, al); 391 | } 392 | else 393 | return errorMess(s); 394 | return ""; 395 | }// checkC() 396 | 397 | private String checkC(String s, byte code, ArrayList AL, int which) { 398 | if (which < AL.size()) { 399 | AdvLexem al = (AdvLexem)AL.get(which); 400 | if (al.code != code) 401 | return errorMess(s, al); 402 | } 403 | else 404 | return errorMess(s); 405 | return ""; 406 | }// checkC() 407 | 408 | // checks if there is a lexem of given Lower category 409 | private String checkL(String s, byte code, ArrayList AL) { 410 | if (pos < AL.size()) { 411 | AdvLexem al = (AdvLexem)AL.get(pos); 412 | if (al.lcategory != code) 413 | return errorMess(s, al); 414 | } 415 | else 416 | return errorMess(s); 417 | return ""; 418 | }// checkL() 419 | 420 | // mmmmmmmmmmmmmmmmmmm VALIDATION mmmmmmmmmmmmmmmmmmmmmmmmmmm 421 | 422 | public String checkStructure(ArrayList AL) { 423 | Codes codes = new Codes(); 424 | codes.fillLists(); 425 | String result = ""; 426 | AdvLexem al; 427 | int size = AL.size(); 428 | result = checkC("PROGRAM", codes.PROGRAM, AL); 429 | if (!(result.equals(""))) 430 | return result; 431 | pos ++; 432 | result = checkC("VARIABLE", codes.VARIABLE, AL); 433 | if (!(result.equals(""))) 434 | return result; 435 | pos ++; 436 | result = checkC("SEMICOLON", codes.SEMICOLON, AL); 437 | if (!(result.equals(""))) 438 | return result; 439 | pos ++; 440 | result = checkC("VAR", codes.VAR, AL); 441 | if (!(result.equals(""))) { 442 | result = checkC("VAR or BEGIN", codes.BEGIN, AL); 443 | if (!(result.equals(""))) 444 | return result; 445 | } 446 | if ((((AdvLexem)AL.get(pos)).value).equals("VAR")) { 447 | pos ++; 448 | result = checkVAR(AL); 449 | if (!(result.equals(""))) 450 | return result; 451 | } 452 | 453 | // now we check if there is END. at the end of the program 454 | if (AL.size() - 1 > pos) { 455 | result = checkC("END.", Codes.END_DOT, AL, AL.size() - 1); 456 | if (!(result.equals(""))) 457 | return result; 458 | } 459 | else 460 | return "expected: END. at the end of program"; 461 | 462 | // now we check if all VARIABLES are declared 463 | for (int i = pos + 1; i < AL.size() - 2; i ++) { 464 | al = (AdvLexem)(AL.get(i)); 465 | if (al.code == Codes.VARIABLE) 466 | if (!(vars.contains(al.value))) 467 | return "cannot resolve symbol variable " + al.value 468 | + "\n line: " + al.line 469 | + "\n column: " + al.column; 470 | } 471 | 472 | pos ++; 473 | main_begin = pos; 474 | result = checkBody(AL); 475 | 476 | /* 477 | //testing... 478 | System.out.println("Position = " + pos); 479 | al = (AdvLexem)AL.get(pos); 480 | System.out.println("Lexem value = " + al.value); 481 | */ 482 | 483 | if (!(result.equals(""))) 484 | return result; 485 | 486 | return "No errors !"; 487 | }// checkStructure 488 | 489 | private String checkVAR(ArrayList AL) { 490 | byte var_count = 0; 491 | String result = ""; 492 | AdvLexem al = null; 493 | // if BEGIN appears changes to TRUE 494 | boolean finish_b = false; 495 | // if there is no COMMA changes to TRUE 496 | boolean finish_c = false; 497 | 498 | while (!finish_b) { 499 | result = checkC("BEGIN", Codes.BEGIN, AL); 500 | // if there is no BEGIN yet 501 | if (!result.equals("")) { 502 | result = checkC("VARIABLE", Codes.VARIABLE, AL); 503 | // if there is a VARIABLE 504 | if (!result.equals("")) 505 | return result; 506 | al = (AdvLexem)AL.get(pos); 507 | if ((((AdvLexem)AL.get(1)).value).equals(al.value)) 508 | return "VARIABLE is already used as a neme of the program" 509 | + "\n line: " + al.line 510 | + "\n column: " + al.column; 511 | if (vars.contains(al.value)) 512 | return ("VARIABLE already declared: " + al.value 513 | + "\n line: " + al.line 514 | + "\n column: " + al.column); 515 | vars.add(al.value); 516 | var_count ++; 517 | finish_c = false; 518 | while (!finish_c) { 519 | pos ++; 520 | result = checkC("COMMA", Codes.COMMA, AL); 521 | if (result.equals("")) { 522 | pos ++; 523 | result = checkC("VARIABLE", Codes.VARIABLE, AL); 524 | if (!result.equals("")) 525 | return result; 526 | al = (AdvLexem)AL.get(pos); 527 | if ((((AdvLexem)AL.get(1)).value).equals(al.value)) 528 | return "VARIABLE is already used as a neme " 529 | + "of the program" 530 | + "\n line: " + al.line 531 | + "\n column: " + al.column; 532 | if (vars.contains(al.value)) 533 | return ("VARIABLE already declared: " + al.value 534 | + "\n line: " + al.line 535 | + "\n column: " + al.column); 536 | vars.add(al.value); 537 | var_count ++; 538 | } 539 | else 540 | finish_c = true; 541 | }// while 542 | result = checkC("COLON", Codes.COLON, AL); 543 | if (!result.equals("")) 544 | return result; 545 | pos ++; 546 | result = checkL("VARIABLE TYPE", Codes.VTYPE, AL); 547 | if (!result.equals("")) 548 | return result; 549 | 550 | byte code = ((AdvLexem)AL.get(pos)).code; 551 | for (byte i = 0; i < var_count; i ++) 552 | var_types.add(new Integer(code)); 553 | var_count = 0; 554 | 555 | pos ++; 556 | result = checkC("SEMICOLON", Codes.SEMICOLON, AL); 557 | if (!result.equals("")) 558 | return result; 559 | pos ++; 560 | } 561 | else 562 | finish_b = true; 563 | }// while 564 | return ""; 565 | }// checkVAR() 566 | 567 | private String checkIASSOC(ArrayList AL) { 568 | String result = ""; 569 | AdvLexem al = null; 570 | byte b_count = 0; // brackets count 571 | boolean finish_b = false; // finish if no more brackets 572 | 573 | while (!finish_b) { 574 | result = checkC("VALUE", Codes.LBRACKET, AL); 575 | if (!result.equals("")) 576 | finish_b = true; 577 | else { 578 | b_count ++; 579 | pos ++; 580 | } 581 | } 582 | result = checkC("VALUE", Codes.MINUS, AL); 583 | if (result.equals("")) { 584 | pos ++; 585 | result = checkC("INTEGER NUMBER", Codes.INUMBER, AL); 586 | if (result.equals("")) { 587 | al = (AdvLexem)AL.get(pos); 588 | AL.set(pos - 1, new AdvLexem("-" + al.value, al.code, 589 | al.lcategory, al.hcategory, 590 | al.line, al.column)); 591 | AL.remove(pos); 592 | } 593 | else 594 | return result; 595 | } 596 | else { 597 | result = checkC("INTEGER VALUE", Codes.INUMBER, AL); 598 | if (!result.equals("")) { 599 | result = checkC("INTEGER VARIABLE", Codes.VARIABLE, AL); 600 | if (!result.equals("")) 601 | return result; 602 | else { 603 | al = (AdvLexem)AL.get(pos); 604 | int tmp = vars.indexOf(al.value); 605 | if (!(((Integer)var_types.get(tmp)).equals( 606 | new Integer(Codes.INTEGER)))) { 607 | al = (AdvLexem)AL.get(pos); 608 | return "INTEGER VARIABLE expected " 609 | + "\n found: " + al.value 610 | + "\n line: " + al.line 611 | + "\n column: " + al.column; 612 | } 613 | else 614 | pos++; 615 | } 616 | } 617 | else 618 | pos ++; 619 | } 620 | finish_b = false; 621 | while (!finish_b) { 622 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 623 | if (!result.equals("")) 624 | finish_b = true; 625 | else { 626 | if (b_count > 0) { 627 | b_count --; 628 | pos ++; 629 | } 630 | else { 631 | al = (AdvLexem)AL.get(pos); 632 | return "RIGHT BRACKET not opened" 633 | + "\n line: " + al.line 634 | + "\n column: " + al.column; 635 | } 636 | } 637 | } 638 | boolean finish_o = false; // if there is no operator 639 | while (!finish_o){ 640 | result = checkL("ARITHMETIC OPERATOR", Codes.AOPERATOR, AL); 641 | if (!result.equals("")) 642 | finish_o = true; 643 | else { 644 | pos ++; 645 | finish_b = false; 646 | while (!finish_b) { 647 | result = checkC("VALUE", Codes.LBRACKET, AL); 648 | if (!result.equals("")) 649 | finish_b = true; 650 | else { 651 | b_count ++; 652 | pos ++; 653 | } 654 | } 655 | result = checkC("VALUE", Codes.MINUS, AL); 656 | if (result.equals("")) { 657 | pos ++; 658 | result = checkC("INTEGER NUMBER", Codes.INUMBER, AL); 659 | if (result.equals("")) { 660 | al = (AdvLexem)AL.get(pos); 661 | AL.set(pos - 1, new AdvLexem("-" + al.value, al.code, 662 | al.lcategory, 663 | al.hcategory, 664 | al.line, al.column)); 665 | AL.remove(pos); 666 | } 667 | else 668 | return result; 669 | } 670 | else { 671 | result = checkC("INTEGER VALUE", Codes.INUMBER, AL); 672 | if (!result.equals("")) { 673 | result = checkC("INTEGER VARIABLE", Codes.VARIABLE, AL); 674 | if (!result.equals("")) 675 | return result; 676 | else { 677 | al = (AdvLexem)AL.get(pos); 678 | int tmp = vars.indexOf(al.value); 679 | if (!(((Integer)var_types.get(tmp)).equals( 680 | new Integer(Codes.INTEGER)))) { 681 | al = (AdvLexem)AL.get(pos); 682 | return "INTEGER VARIABLE expected " 683 | + "\n found: " + al.value 684 | + "\n line: " + al.line 685 | + "\n column: " + al.column; 686 | } 687 | else 688 | pos++; 689 | } 690 | } 691 | else 692 | pos ++; 693 | } 694 | finish_b = false; 695 | while (!finish_b) { 696 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 697 | if (!result.equals("")) 698 | finish_b = true; 699 | else { 700 | if (b_count > 0) { 701 | b_count --; 702 | pos ++; 703 | } 704 | else { 705 | al = (AdvLexem)AL.get(pos); 706 | return "RIGHT BRACKET not opened" 707 | + "\n line: " + al.line 708 | + "\n column: " + al.column; 709 | } 710 | } 711 | } 712 | } 713 | } 714 | 715 | return ""; 716 | }// checkIASSOC() 717 | 718 | private String checkRASSOC(ArrayList AL) { 719 | String result = ""; 720 | AdvLexem al = null; 721 | byte b_count = 0; // brackets count 722 | boolean finish_b = false; // finish if no more brackets 723 | 724 | while (!finish_b) { 725 | result = checkC("VALUE", Codes.LBRACKET, AL); 726 | if (!result.equals("")) 727 | finish_b = true; 728 | else { 729 | b_count ++; 730 | pos ++; 731 | } 732 | } 733 | result = checkC("VALUE", Codes.MINUS, AL); 734 | if (result.equals("")) { 735 | pos ++; 736 | result = checkC("INTEGER OR REAL NUMBER", Codes.INUMBER, AL); 737 | if ((result.equals("")) || 738 | (checkC("", Codes.RNUMBER, AL).equals(""))) { 739 | al = (AdvLexem)AL.get(pos); 740 | AL.set(pos - 1, new AdvLexem("-" + al.value, al.code, 741 | al.lcategory, al.hcategory, 742 | al.line, al.column)); 743 | AL.remove(pos); 744 | } 745 | else 746 | return result; 747 | } 748 | else { 749 | result = checkC("", Codes.INUMBER, AL); 750 | if (!((result.equals("")) || 751 | (checkC("", Codes.RNUMBER, AL).equals("")))) { 752 | result = checkC("INTEGER OR REAL VARIABLE", 753 | Codes.VARIABLE, AL); 754 | if (!result.equals("")) 755 | return result; 756 | else { 757 | al = (AdvLexem)AL.get(pos); 758 | int tmp = vars.indexOf(al.value); 759 | int type = ((Integer)var_types.get(tmp)).intValue(); 760 | if (!((type == Codes.INTEGER) || (type == Codes.REAL))) { 761 | al = (AdvLexem)AL.get(pos); 762 | return "INTEGER OR REAL VARIABLE expected " 763 | + "\n found: " + al.value 764 | + "\n line: " + al.line 765 | + "\n column: " + al.column; 766 | } 767 | else 768 | pos++; 769 | } 770 | } 771 | else 772 | pos ++; 773 | } 774 | finish_b = false; 775 | while (!finish_b) { 776 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 777 | if (!result.equals("")) 778 | finish_b = true; 779 | else { 780 | if (b_count > 0) { 781 | b_count --; 782 | pos ++; 783 | } 784 | else { 785 | al = (AdvLexem)AL.get(pos); 786 | return "RIGHT BRACKET not opened" 787 | + "\n line: " + al.line 788 | + "\n column: " + al.column; 789 | } 790 | } 791 | } 792 | boolean finish_o = false; // if there is no operator 793 | while (!finish_o){ 794 | result = checkL("closing BRACKET", Codes.AOPERATOR, AL); 795 | if (!result.equals("")) { 796 | finish_o = true; 797 | if (b_count > 0) 798 | return result; 799 | } 800 | else { 801 | pos ++; 802 | finish_b = false; 803 | while (!finish_b) { 804 | result = checkC("VALUE", Codes.LBRACKET, AL); 805 | if (!result.equals("")) 806 | finish_b = true; 807 | else { 808 | b_count ++; 809 | pos ++; 810 | } 811 | } 812 | result = checkC("VALUE", Codes.MINUS, AL); 813 | if (result.equals("")) { 814 | pos ++; 815 | result = checkC("INTEGER OR REAL NUMBER", 816 | Codes.INUMBER, AL); 817 | if ((result.equals("")) || 818 | (checkC("", Codes.RNUMBER, AL).equals(""))) { 819 | al = (AdvLexem)AL.get(pos); 820 | AL.set(pos - 1, new AdvLexem("-" + al.value, al.code, 821 | al.lcategory, 822 | al.hcategory, 823 | al.line, al.column)); 824 | AL.remove(pos); 825 | } 826 | else 827 | return result; 828 | } 829 | else { 830 | result = checkC("", Codes.INUMBER, AL); 831 | if (!((result.equals("")) || 832 | (checkC("", Codes.RNUMBER, AL).equals("")))) { 833 | result = checkC("INTEGER OR REAL VARIABLE", 834 | Codes.VARIABLE, AL); 835 | if (!result.equals("")) 836 | return result; 837 | else { 838 | al = (AdvLexem)AL.get(pos); 839 | int tmp = vars.indexOf(al.value); 840 | int type = ((Integer)var_types.get(tmp)) 841 | .intValue(); 842 | if (!((type == Codes.INTEGER) || 843 | (type == Codes.REAL))) { 844 | al = (AdvLexem)AL.get(pos); 845 | return "INTEGER OR REAL VARIABLE expected " 846 | + "\n found: " + al.value 847 | + "\n line: " + al.line 848 | + "\n column: " + al.column; 849 | } 850 | else 851 | pos++; 852 | } 853 | } 854 | else 855 | pos ++; 856 | } 857 | finish_b = false; 858 | while (!finish_b) { 859 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 860 | if (!result.equals("")) 861 | finish_b = true; 862 | else { 863 | if (b_count > 0) { 864 | b_count --; 865 | pos ++; 866 | } 867 | else { 868 | al = (AdvLexem)AL.get(pos); 869 | return "RIGHT BRACKET not opened" 870 | + "\n line: " + al.line 871 | + "\n column: " + al.column; 872 | } 873 | } 874 | } 875 | } 876 | } 877 | return ""; 878 | }// checkRASSOC() 879 | 880 | private String checkLEXPR(ArrayList AL) { 881 | String result = ""; 882 | AdvLexem al = null; 883 | byte b_count = 0; // brackets count 884 | boolean finish_o = false; 885 | boolean finish_n = false; 886 | boolean finish_b = false; 887 | 888 | while (!finish_n) { 889 | finish_b = false; 890 | while (!finish_b) { 891 | result = checkC("VALUE", Codes.LBRACKET, AL); 892 | if (!result.equals("")) 893 | finish_b = true; 894 | else { 895 | b_count ++; 896 | pos ++; 897 | } 898 | } 899 | result = checkC("NOT", Codes.NOT, AL); 900 | if (!result.equals("")) 901 | finish_n = true; 902 | else 903 | pos ++; 904 | } 905 | result = checkLEXPRNElem(AL); 906 | if (result.equals("")) { 907 | result = checkL("RELATION OPERATOR", 908 | Codes.ROPERATOR, AL); 909 | if (!result.equals("")) 910 | return result; 911 | else { 912 | pos ++; 913 | result = checkLEXPRNElem(AL); 914 | if (!result.equals("")) 915 | return result; 916 | } 917 | } 918 | else { 919 | result = checkLEXPRLElem(AL); 920 | if (!result.equals("")) 921 | return result; 922 | } 923 | finish_b = false; 924 | while (!finish_b) { 925 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 926 | if (!result.equals("")) 927 | finish_b = true; 928 | else { 929 | if (b_count > 0) { 930 | b_count --; 931 | pos ++; 932 | } 933 | else { 934 | al = (AdvLexem)AL.get(pos); 935 | return "RIGHT BRACKET not opened" 936 | + "\n line: " + al.line 937 | + "\n column: " + al.column; 938 | } 939 | } 940 | } 941 | finish_o = false; 942 | while (!finish_o){ 943 | result = checkL("closing BRACKET", Codes.LOPERATOR, AL); 944 | if (!result.equals("")) { 945 | finish_o = true; 946 | if (b_count > 0) 947 | return result; 948 | } 949 | else { 950 | pos ++; 951 | finish_n = false; 952 | while (!finish_n) { 953 | finish_b = false; 954 | while (!finish_b) { 955 | result = checkC("VALUE", Codes.LBRACKET, AL); 956 | if (!result.equals("")) 957 | finish_b = true; 958 | else { 959 | b_count ++; 960 | pos ++; 961 | } 962 | } 963 | result = checkC("NOT", Codes.NOT, AL); 964 | if (!result.equals("")) 965 | finish_n = true; 966 | else 967 | pos ++; 968 | } 969 | result = checkLEXPRNElem(AL); 970 | if (result.equals("")) { 971 | result = checkL("RELATION OPERATOR", 972 | Codes.ROPERATOR, AL); 973 | if (!result.equals("")) 974 | return result; 975 | else { 976 | pos ++; 977 | result = checkLEXPRNElem(AL); 978 | if (!result.equals("")) 979 | return result; 980 | } 981 | } 982 | else { 983 | result = checkLEXPRLElem(AL); 984 | if (!result.equals("")) 985 | return result; 986 | } 987 | finish_b = false; 988 | while (!finish_b) { 989 | result = checkC("OPERATOR", Codes.RBRACKET, AL); 990 | if (!result.equals("")) 991 | finish_b = true; 992 | else { 993 | if (b_count > 0) { 994 | b_count --; 995 | pos ++; 996 | } 997 | else { 998 | al = (AdvLexem)AL.get(pos); 999 | return "RIGHT BRACKET not opened" 1000 | + "\n line: " + al.line 1001 | + "\n column: " + al.column; 1002 | } 1003 | } 1004 | } 1005 | } 1006 | } 1007 | return ""; 1008 | }// checkLEXPR() 1009 | 1010 | private String checkWRITE(ArrayList AL) { 1011 | String result = ""; 1012 | AdvLexem al = null; 1013 | result = checkC("LEFT BRACKET", Codes.LBRACKET, AL); 1014 | if (!result.equals("")) 1015 | return result; 1016 | pos ++; 1017 | result = checkC("STRING or VARIABLE", Codes.STRING, AL); 1018 | if (!(result.equals("") || 1019 | (checkC("", Codes.VARIABLE, AL)).equals(""))) 1020 | return result; 1021 | pos ++; 1022 | boolean finish_c = false; 1023 | while (!finish_c) { 1024 | result = checkC("COMMA", Codes.COMMA, AL); 1025 | if (!result.equals("")) 1026 | finish_c = true; 1027 | else { 1028 | pos ++; 1029 | result = checkC("STRING or VARIABLE", Codes.STRING, AL); 1030 | if (!(result.equals("") || 1031 | (checkC("", Codes.VARIABLE, AL)).equals(""))) 1032 | return result; 1033 | else 1034 | pos ++; 1035 | } 1036 | } 1037 | result = checkC("RIGHT BRACKET", Codes.RBRACKET, AL); 1038 | if (!result.equals("")) 1039 | return result; 1040 | pos ++; 1041 | return ""; 1042 | }// checkWRITE() 1043 | 1044 | private String checkREAD(ArrayList AL) { 1045 | String result = ""; 1046 | AdvLexem al = null; 1047 | result = checkC("LEFT BRACKET", Codes.LBRACKET, AL); 1048 | if (!result.equals("")) 1049 | return result; 1050 | pos ++; 1051 | result = checkC("VARIABLE", Codes.VARIABLE, AL); 1052 | if (!result.equals("")) 1053 | return result; 1054 | pos ++; 1055 | result = checkC("RIGHT BRACKET", Codes.RBRACKET, AL); 1056 | if (!result.equals("")) 1057 | return result; 1058 | pos ++; 1059 | return ""; 1060 | }// checkREAD() 1061 | 1062 | 1063 | //mmmmmmmmmmmmmmmmmm MAIN VALIDATOR mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm 1064 | 1065 | private String checkBody(ArrayList AL) { 1066 | String result = ""; 1067 | AdvLexem al = null; 1068 | int begins = 0; 1069 | 1070 | while (true) { 1071 | al = (AdvLexem)AL.get(pos); 1072 | if (al.code == Codes.VARIABLE) { 1073 | pos ++; 1074 | result = checkC("ASSOCIATE MARK", Codes.ASOCIATE, AL); 1075 | if (!result.equals("")) 1076 | return result; 1077 | pos ++; 1078 | int tmp = vars.indexOf(al.value); 1079 | int type = ((Integer)var_types.get(tmp)).intValue(); 1080 | if (type == Codes.INTEGER) { 1081 | result = checkIASSOC(AL); 1082 | if (!result.equals("")) 1083 | return result; 1084 | } 1085 | else if (type == Codes.REAL) { 1086 | result = checkRASSOC(AL); 1087 | if (!result.equals("")) 1088 | return result; 1089 | } 1090 | else if (type == Codes.BOOLEAN) { 1091 | result = checkLEXPR(AL); 1092 | if (!result.equals("")) 1093 | return result; 1094 | } 1095 | result = checkEnding(AL); 1096 | if (!result.equals("")) 1097 | return result; 1098 | } 1099 | else if (al.code == Codes.WRITELN) { 1100 | pos ++; 1101 | result = checkWRITE(AL); 1102 | if (!result.equals("")) 1103 | return result; 1104 | result = checkEnding(AL); 1105 | if (!result.equals("")) 1106 | return result; 1107 | } 1108 | else if (al.code == Codes.READLN) { 1109 | pos ++; 1110 | result = checkREAD(AL); 1111 | if (!result.equals("")) 1112 | return result; 1113 | result = checkEnding(AL); 1114 | if (!result.equals("")) 1115 | return result; 1116 | } 1117 | else if (al.code == Codes.WHILE) { 1118 | pos ++; 1119 | result = checkLEXPR(AL); 1120 | if (!result.equals("")) 1121 | return result; 1122 | result = checkC("keyword DO", Codes.DO, AL); 1123 | if (!result.equals("")) 1124 | return result; 1125 | pos ++; 1126 | result = checkOne(AL); 1127 | if (!result.equals("")) 1128 | return result; 1129 | } 1130 | else if (al.code == Codes.IF) { 1131 | pos ++; 1132 | result = checkLEXPR(AL); 1133 | if (!result.equals("")) 1134 | return result; 1135 | result = checkC("keyword THEN", Codes.THEN, AL); 1136 | if (!result.equals("")) 1137 | return result; 1138 | pos ++; 1139 | result = checkOne(AL); 1140 | if (!result.equals("")) 1141 | return result; 1142 | result = checkC("keyword ELSE", Codes.ELSE, AL); 1143 | if (result.equals("")) { 1144 | pos ++; 1145 | result = checkOne(AL); 1146 | if (!result.equals("")) 1147 | return result; 1148 | } 1149 | } 1150 | else if (al.code == Codes.BEGIN) { 1151 | AL.remove(pos); 1152 | result = checkBlock(AL); 1153 | if (!result.equals("")) 1154 | return result; 1155 | else { 1156 | pos --; 1157 | result = checkC("SEMICOLON", Codes.SEMICOLON, AL); 1158 | if (result.equals("")) { 1159 | AL.remove(pos); 1160 | pos --; 1161 | } 1162 | AL.remove(pos); 1163 | 1164 | } 1165 | } 1166 | else if (al.code == Codes.END) { 1167 | return "END without coresponding BEGIN" 1168 | + "\n found: " + al.value 1169 | + "\n line: " + al.line 1170 | + "\n column: " + al.column; 1171 | } 1172 | else if (al.code == Codes.END_DOT) { 1173 | return ""; 1174 | } 1175 | else 1176 | return "Not a statement" 1177 | + "\n found: " + al.value 1178 | + "\n line: " + al.line 1179 | + "\n column: " + al.column; 1180 | } 1181 | }// checkBODY() 1182 | 1183 | private String checkBlock(ArrayList AL) { 1184 | String result = ""; 1185 | AdvLexem al = null; 1186 | int begins = 0; 1187 | 1188 | while (true) { 1189 | al = (AdvLexem)AL.get(pos); 1190 | if (al.code == Codes.VARIABLE) { 1191 | pos ++; 1192 | result = checkC("ASSOCIATE MARK", Codes.ASOCIATE, AL); 1193 | if (!result.equals("")) 1194 | return result; 1195 | pos ++; 1196 | int tmp = vars.indexOf(al.value); 1197 | int type = ((Integer)var_types.get(tmp)).intValue(); 1198 | if (type == Codes.INTEGER) { 1199 | result = checkIASSOC(AL); 1200 | if (!result.equals("")) 1201 | return result; 1202 | } 1203 | else if (type == Codes.REAL) { 1204 | result = checkRASSOC(AL); 1205 | if (!result.equals("")) 1206 | return result; 1207 | } 1208 | else if (type == Codes.BOOLEAN) { 1209 | result = checkLEXPR(AL); 1210 | if (!result.equals("")) 1211 | return result; 1212 | } 1213 | result = checkEnding(AL); 1214 | if (!result.equals("")) 1215 | return result; 1216 | } 1217 | else if (al.code == Codes.WRITELN) { 1218 | pos ++; 1219 | result = checkWRITE(AL); 1220 | if (!result.equals("")) 1221 | return result; 1222 | result = checkEnding(AL); 1223 | if (!result.equals("")) 1224 | return result; 1225 | } 1226 | else if (al.code == Codes.READLN) { 1227 | pos ++; 1228 | result = checkREAD(AL); 1229 | if (!result.equals("")) 1230 | return result; 1231 | result = checkEnding(AL); 1232 | if (!result.equals("")) 1233 | return result; 1234 | } 1235 | else if (al.code == Codes.WHILE) { 1236 | pos ++; 1237 | result = checkLEXPR(AL); 1238 | if (!result.equals("")) 1239 | return result; 1240 | result = checkC("keyword DO", Codes.DO, AL); 1241 | if (!result.equals("")) 1242 | return result; 1243 | pos ++; 1244 | result = checkOne(AL); 1245 | if (!result.equals("")) 1246 | return result; 1247 | } 1248 | else if (al.code == Codes.IF) { 1249 | pos ++; 1250 | result = checkLEXPR(AL); 1251 | if (!result.equals("")) 1252 | return result; 1253 | result = checkC("keyword THEN", Codes.THEN, AL); 1254 | if (!result.equals("")) 1255 | return result; 1256 | pos ++; 1257 | result = checkOne(AL); 1258 | if (!result.equals("")) 1259 | return result; 1260 | result = checkC("keyword ELSE", Codes.ELSE, AL); 1261 | if (result.equals("")) { 1262 | pos ++; 1263 | result = checkOne(AL); 1264 | if (!result.equals("")) 1265 | return result; 1266 | } 1267 | } 1268 | else if (al.code == Codes.BEGIN) { 1269 | pos ++; 1270 | result = checkBlock(AL); 1271 | if (!result.equals("")) 1272 | return result; 1273 | } 1274 | else if (al.code == Codes.END) { 1275 | pos ++; 1276 | result = checkEnding(AL); 1277 | if (!result.equals("")) 1278 | return result; 1279 | else 1280 | return ""; 1281 | } 1282 | else if (al.code == Codes.END_DOT) { 1283 | return "END expected" 1284 | + "\n line: " + al.line 1285 | + "\n column: " + al.column; 1286 | } 1287 | else 1288 | return "Not a statement" 1289 | + "\n found: " + al.value 1290 | + "\n line: " + al.line 1291 | + "\n column: " + al.column; 1292 | } 1293 | }// checkBlock() 1294 | 1295 | private String checkOne(ArrayList AL) { 1296 | String result = ""; 1297 | AdvLexem al = null; 1298 | int begins = 0; 1299 | al = (AdvLexem)AL.get(pos); 1300 | if (al.code == Codes.VARIABLE) { 1301 | 1302 | pos ++; 1303 | result = checkC("ASSOCIATE MARK", Codes.ASOCIATE, AL); 1304 | if (!result.equals("")) 1305 | return result; 1306 | pos ++; 1307 | int tmp = vars.indexOf(al.value); 1308 | int type = ((Integer)var_types.get(tmp)).intValue(); 1309 | 1310 | if (type == Codes.INTEGER) { 1311 | result = checkIASSOC(AL); 1312 | if (!result.equals("")) 1313 | return result; 1314 | } 1315 | else if (type == Codes.REAL) { 1316 | result = checkRASSOC(AL); 1317 | if (!result.equals("")) 1318 | return result; 1319 | } 1320 | else if (type == Codes.BOOLEAN) { 1321 | result = checkLEXPR(AL); 1322 | if (!result.equals("")) 1323 | return result; 1324 | } 1325 | return checkEnding(AL); 1326 | } 1327 | else if (al.code == Codes.WRITELN) { 1328 | pos ++; 1329 | result = checkWRITE(AL); 1330 | if (!result.equals("")) 1331 | return result; 1332 | return checkEnding(AL); 1333 | } 1334 | else if (al.code == Codes.READLN) { 1335 | pos ++; 1336 | result = checkREAD(AL); 1337 | if (!result.equals("")) 1338 | return result; 1339 | return checkEnding(AL); 1340 | } 1341 | else if (al.code == Codes.WHILE) { 1342 | pos ++; 1343 | result = checkLEXPR(AL); 1344 | if (!result.equals("")) 1345 | return result; 1346 | result = checkC("keyword DO", Codes.DO, AL); 1347 | if (!result.equals("")) 1348 | return result; 1349 | pos ++; 1350 | return checkOne(AL); 1351 | } 1352 | else if (al.code == Codes.IF) { 1353 | pos ++; 1354 | result = checkLEXPR(AL); 1355 | if (!result.equals("")) 1356 | return result; 1357 | result = checkC("keyword THEN", Codes.THEN, AL); 1358 | if (!result.equals("")) 1359 | return result; 1360 | pos ++; 1361 | 1362 | result = checkOne(AL); 1363 | if (!result.equals("")) 1364 | return result; 1365 | result = checkC("keyword ELSE", Codes.ELSE, AL); 1366 | if (result.equals("")) { 1367 | pos ++; 1368 | return checkOne(AL); 1369 | } 1370 | else 1371 | return ""; 1372 | } 1373 | else if (al.code == Codes.BEGIN) { 1374 | pos ++; 1375 | 1376 | return checkBlock(AL); 1377 | } 1378 | else if (al.code == Codes.END) { 1379 | return "END without coresponding BEGIN" 1380 | + "\n found: " + al.value 1381 | + "\n line: " + al.line 1382 | + "\n column: " + al.column; 1383 | } 1384 | else if ((al.code == Codes.END_DOT) || 1385 | (al.code == Codes.ELSE)) { 1386 | return "INSTRUCTION expected" 1387 | + "\n line: " + al.line 1388 | + "\n column: " + al.column; 1389 | } 1390 | else 1391 | return "Not a statement" 1392 | + "\n found: " + al.value 1393 | + "\n line: " + al.line 1394 | + "\n column: " + al.column; 1395 | }// checkOne() 1396 | 1397 | private String checkEnding(ArrayList AL) { 1398 | String result = ""; 1399 | 1400 | result = checkC("SEMICOLON", Codes.SEMICOLON, AL); 1401 | if (!result.equals("")) { 1402 | result = checkC("SEMICOLON", Codes.END, AL); 1403 | if (!result.equals("")) { 1404 | result = checkC("SEMICOLON", Codes.ELSE, AL); 1405 | if (!result.equals("")) { 1406 | result = checkC("SEMICOLON", Codes.END_DOT, AL); 1407 | if (!result.equals("")) 1408 | return result; 1409 | } 1410 | } 1411 | } 1412 | else 1413 | pos ++; 1414 | return ""; 1415 | 1416 | }// checkEnding() 1417 | 1418 | // checks Logical EXPRexion Number Element 1419 | private String checkLEXPRNElem(ArrayList AL) { 1420 | String result = ""; 1421 | AdvLexem al = null; 1422 | 1423 | result = checkC("VALUE", Codes.MINUS, AL); 1424 | if (result.equals("")) { 1425 | pos ++; 1426 | result = checkC("INTEGER OR REAL NUMBER", 1427 | Codes.INUMBER, AL); 1428 | if ((result.equals("")) || 1429 | (checkC("", Codes.RNUMBER, AL).equals(""))) { 1430 | al = (AdvLexem)AL.get(pos); 1431 | AL.set(pos - 1, new AdvLexem("-" + al.value, al.code, 1432 | al.lcategory, 1433 | al.hcategory, 1434 | al.line, al.column)); 1435 | AL.remove(pos); 1436 | return ""; 1437 | } 1438 | else 1439 | return result; 1440 | } 1441 | else { 1442 | result = checkC("", Codes.INUMBER, AL); 1443 | if (!((result.equals("")) || 1444 | (checkC("", Codes.RNUMBER, AL).equals("")))) { 1445 | result = checkC("INTEGER OR REAL VARIABLE", 1446 | Codes.VARIABLE, AL); 1447 | if (!result.equals("")) 1448 | return result; 1449 | else { 1450 | al = (AdvLexem)AL.get(pos); 1451 | int tmp = vars.indexOf(al.value); 1452 | int type = ((Integer)var_types.get(tmp)).intValue(); 1453 | if (!((type == Codes.INTEGER) || (type == Codes.REAL))) { 1454 | al = (AdvLexem)AL.get(pos); 1455 | return "INTEGER OR REAL VARIABLE expected " 1456 | + "\n found: " + al.value 1457 | + "\n line: " + al.line 1458 | + "\n column: " + al.column; 1459 | } 1460 | else { 1461 | pos++; 1462 | return ""; 1463 | } 1464 | } 1465 | } 1466 | else { 1467 | pos ++; 1468 | return ""; 1469 | } 1470 | } 1471 | }// checkLEXPRNElem() 1472 | 1473 | // checks Logical EXPRexion Logic Element 1474 | private String checkLEXPRLElem(ArrayList AL) { 1475 | String result = ""; 1476 | AdvLexem al = null; 1477 | result = checkC("", Codes.TRUE, AL); 1478 | if (!((result.equals("")) || 1479 | (checkC("", Codes.FALSE, AL).equals("")))) { 1480 | result = checkC("LOGICAL VALUE", 1481 | Codes.VARIABLE, AL); 1482 | if (!result.equals("")) 1483 | return result; 1484 | else { 1485 | al = (AdvLexem)AL.get(pos); 1486 | int tmp = vars.indexOf(al.value); 1487 | int type = ((Integer)var_types.get(tmp)).intValue(); 1488 | if (!(type == Codes.BOOLEAN)) { 1489 | al = (AdvLexem)AL.get(pos); 1490 | return "expected: LOGICAL VALUE" 1491 | + "\n found: " + al.value 1492 | + "\n line: " + al.line 1493 | + "\n column: " + al.column; 1494 | } 1495 | else { 1496 | pos++; 1497 | return ""; 1498 | } 1499 | } 1500 | } 1501 | else { 1502 | pos ++; 1503 | return ""; 1504 | } 1505 | }// checkLEXPRLElem() 1506 | 1507 | //mmmmmmmmmmmmmmmmmmmmmm INTERPRETER mmmmmmmmmmmmmmmmmmmmmmmmmmm 1508 | 1509 | private ArrayList interBody(ArrayList AL) { 1510 | ArrayList baze = new ArrayList(); 1511 | String itype = ""; 1512 | String variable = ""; 1513 | int start = 0; 1514 | int finish = 0; 1515 | ArrayList iftrue = new ArrayList(); 1516 | ArrayList ifelse = new ArrayList(); 1517 | 1518 | AdvLexem al = null; 1519 | 1520 | while (true) { 1521 | al = (AdvLexem)AL.get(pos); 1522 | if (al.code == Codes.VARIABLE) { 1523 | variable = al.value; 1524 | pos ++; 1525 | pos ++; 1526 | start = pos; 1527 | int tmp = vars.indexOf(al.value); 1528 | int type = ((Integer)var_types.get(tmp)).intValue(); 1529 | 1530 | if (type == Codes.INTEGER) { 1531 | checkIASSOC(AL); 1532 | finish = pos - 1; 1533 | itype = "IASSOC"; 1534 | } 1535 | else if (type == Codes.REAL) { 1536 | checkRASSOC(AL); 1537 | finish = pos - 1; 1538 | itype = "RASSOC"; 1539 | } 1540 | else if (type == Codes.BOOLEAN) { 1541 | checkLEXPR(AL); 1542 | finish = pos - 1; 1543 | itype = "BASSOC"; 1544 | } 1545 | checkEnding(AL); 1546 | } 1547 | else if (al.code == Codes.WRITELN) { 1548 | itype = "WRITE"; 1549 | pos ++; 1550 | start = pos; 1551 | checkWRITE(AL); 1552 | finish = pos - 1; 1553 | checkEnding(AL); 1554 | } 1555 | else if (al.code == Codes.READLN) { 1556 | itype = "READ"; 1557 | pos ++; 1558 | start = pos; 1559 | checkREAD(AL); 1560 | finish = pos - 1; 1561 | checkEnding(AL); 1562 | } 1563 | else if (al.code == Codes.WHILE) { 1564 | itype = "WHILE"; 1565 | pos ++; 1566 | start = pos; 1567 | checkLEXPR(AL); 1568 | finish = pos - 1; 1569 | pos ++; 1570 | iftrue = interOne(AL); 1571 | } 1572 | else if (al.code == Codes.IF) { 1573 | itype = "IF"; 1574 | pos ++; 1575 | start = pos; 1576 | checkLEXPR(AL); 1577 | finish = pos - 1; 1578 | pos ++; 1579 | iftrue = interOne(AL); 1580 | if (checkC("keyword ELSE", Codes.ELSE, AL).equals("")) { 1581 | pos ++; 1582 | ifelse = interOne(AL); 1583 | } 1584 | } 1585 | else if (al.code == Codes.BEGIN) { 1586 | pos ++; 1587 | return interBlock(AL); 1588 | 1589 | } 1590 | else if (al.code == Codes.END_DOT) { 1591 | pos ++; 1592 | checkEnding(AL); 1593 | return baze; 1594 | } 1595 | baze.add(new ExecLexem(itype, variable, AL, start, finish, 1596 | iftrue, ifelse)); 1597 | iftrue = new ArrayList(); 1598 | ifelse = new ArrayList(); 1599 | variable = ""; 1600 | } 1601 | }// interBODY() 1602 | 1603 | private ArrayList interBlock(ArrayList AL) { 1604 | ArrayList baze = new ArrayList(); 1605 | String itype = ""; 1606 | String variable = ""; 1607 | int start = 0; 1608 | int finish = 0; 1609 | ArrayList iftrue = new ArrayList(); 1610 | ArrayList ifelse = new ArrayList(); 1611 | 1612 | AdvLexem al = null; 1613 | 1614 | while (true) { 1615 | al = (AdvLexem)AL.get(pos); 1616 | if (al.code == Codes.VARIABLE) { 1617 | variable = al.value; 1618 | pos ++; 1619 | pos ++; 1620 | start = pos; 1621 | int tmp = vars.indexOf(al.value); 1622 | int type = ((Integer)var_types.get(tmp)).intValue(); 1623 | 1624 | if (type == Codes.INTEGER) { 1625 | checkIASSOC(AL); 1626 | finish = pos - 1; 1627 | itype = "IASSOC"; 1628 | } 1629 | else if (type == Codes.REAL) { 1630 | checkRASSOC(AL); 1631 | finish = pos - 1; 1632 | itype = "RASSOC"; 1633 | } 1634 | else if (type == Codes.BOOLEAN) { 1635 | checkLEXPR(AL); 1636 | finish = pos - 1; 1637 | itype = "BASSOC"; 1638 | } 1639 | checkEnding(AL); 1640 | } 1641 | else if (al.code == Codes.WRITELN) { 1642 | itype = "WRITE"; 1643 | pos ++; 1644 | start = pos; 1645 | checkWRITE(AL); 1646 | finish = pos - 1; 1647 | checkEnding(AL); 1648 | } 1649 | else if (al.code == Codes.READLN) { 1650 | itype = "READ"; 1651 | pos ++; 1652 | start = pos; 1653 | checkREAD(AL); 1654 | finish = pos - 1; 1655 | checkEnding(AL); 1656 | } 1657 | else if (al.code == Codes.WHILE) { 1658 | itype = "WHILE"; 1659 | pos ++; 1660 | start = pos; 1661 | checkLEXPR(AL); 1662 | finish = pos - 1; 1663 | pos ++; 1664 | iftrue = interOne(AL); 1665 | } 1666 | else if (al.code == Codes.IF) { 1667 | itype = "IF"; 1668 | pos ++; 1669 | start = pos; 1670 | checkLEXPR(AL); 1671 | finish = pos - 1; 1672 | pos ++; 1673 | iftrue = interOne(AL); 1674 | if (checkC("keyword ELSE", Codes.ELSE, AL).equals("")) { 1675 | pos ++; 1676 | ifelse = interOne(AL); 1677 | } 1678 | } 1679 | else if (al.code == Codes.BEGIN) { 1680 | pos ++; 1681 | return interBlock(AL); 1682 | } 1683 | else if (al.code == Codes.END) { 1684 | pos ++; 1685 | checkEnding(AL); 1686 | return baze; 1687 | } 1688 | baze.add(new ExecLexem(itype, variable, AL, start, finish, 1689 | iftrue, ifelse)); 1690 | iftrue = new ArrayList(); 1691 | ifelse = new ArrayList(); 1692 | variable = ""; 1693 | 1694 | } 1695 | }// interBlock() 1696 | 1697 | private ArrayList interOne(ArrayList AL) { 1698 | ArrayList baze = new ArrayList(); 1699 | String itype = ""; 1700 | String variable = ""; 1701 | int start = 0; 1702 | int finish = 0; 1703 | ArrayList iftrue = new ArrayList(); 1704 | ArrayList ifelse = new ArrayList(); 1705 | 1706 | AdvLexem al = null; 1707 | al = (AdvLexem)AL.get(pos); 1708 | if (al.code == Codes.VARIABLE) { 1709 | variable = al.value; 1710 | pos ++; 1711 | pos ++; 1712 | start = pos; 1713 | int tmp = vars.indexOf(al.value); 1714 | int type = ((Integer)var_types.get(tmp)).intValue(); 1715 | 1716 | if (type == Codes.INTEGER) { 1717 | checkIASSOC(AL); 1718 | finish = pos - 1; 1719 | itype = "IASSOC"; 1720 | } 1721 | else if (type == Codes.REAL) { 1722 | checkRASSOC(AL); 1723 | finish = pos - 1; 1724 | itype = "RASSOC"; 1725 | } 1726 | else if (type == Codes.BOOLEAN) { 1727 | checkLEXPR(AL); 1728 | finish = pos - 1; 1729 | itype = "BASSOC"; 1730 | } 1731 | checkEnding(AL); 1732 | } 1733 | else if (al.code == Codes.WRITELN) { 1734 | itype = "WRITE"; 1735 | pos ++; 1736 | start = pos; 1737 | checkWRITE(AL); 1738 | finish = pos - 1; 1739 | checkEnding(AL); 1740 | } 1741 | else if (al.code == Codes.READLN) { 1742 | itype = "READ"; 1743 | pos ++; 1744 | start = pos; 1745 | checkREAD(AL); 1746 | finish = pos - 1; 1747 | checkEnding(AL); 1748 | } 1749 | else if (al.code == Codes.WHILE) { 1750 | itype = "WHILE"; 1751 | pos ++; 1752 | start = pos; 1753 | checkLEXPR(AL); 1754 | finish = pos - 1; 1755 | pos ++; 1756 | iftrue = interOne(AL); 1757 | } 1758 | else if (al.code == Codes.IF) { 1759 | itype = "IF"; 1760 | pos ++; 1761 | start = pos; 1762 | checkLEXPR(AL); 1763 | finish = pos - 1; 1764 | pos ++; 1765 | iftrue = interOne(AL); 1766 | if (checkC("keyword ELSE", Codes.ELSE, AL).equals("")) { 1767 | pos ++; 1768 | ifelse = interOne(AL); 1769 | } 1770 | } 1771 | else if (al.code == Codes.BEGIN) { 1772 | pos ++; 1773 | return interBlock(AL); 1774 | } 1775 | baze.add(new ExecLexem(itype, variable, AL, start, finish, 1776 | iftrue, ifelse)); 1777 | return baze; 1778 | 1779 | }// checkOne() 1780 | 1781 | //mmmmmmmmmmmmmmmmmmmmmmm EVALUATION mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm 1782 | 1783 | private void initializeVars() { 1784 | for (int i = 0; i < var_types.size(); i++) 1785 | if (((Integer)var_types.get(i)).intValue() == Codes.INTEGER) 1786 | var_vals.add(new Integer(0)); 1787 | else if (((Integer)var_types.get(i)).intValue() == Codes.REAL) 1788 | var_vals.add(new Float(0)); 1789 | else 1790 | var_vals.add(new Boolean(false)); 1791 | }// initializeVars() 1792 | 1793 | private int evalInt(AdvLexem[] lex) { 1794 | ArrayList args = new ArrayList(); 1795 | ArrayList oper = new ArrayList(); 1796 | 1797 | while (place < lex.length) { 1798 | if (lex[place].code == Codes.RBRACKET) { 1799 | break; 1800 | } 1801 | if (lex[place].code == Codes.INUMBER) 1802 | args.add(new Integer(lex[place].value)); 1803 | else if (lex[place].code == Codes.VARIABLE) { 1804 | int tmp = vars.indexOf(lex[place].value); 1805 | args.add((Integer)var_vals.get(tmp)); 1806 | } 1807 | else if (lex[place].code == Codes.LBRACKET) { 1808 | place ++; 1809 | args.add(new Integer(evalInt(lex))); 1810 | } 1811 | else if (lex[place].lcategory == Codes.AOPERATOR) 1812 | oper.add(lex[place].value); 1813 | place ++; 1814 | } 1815 | 1816 | for (int i = 0; i < oper.size(); i++) 1817 | if (((String)oper.get(i)).equals("*")) { 1818 | int tmp1 = ((Integer)args.get(i)).intValue(); 1819 | int tmp2 = ((Integer)args.get(i + 1)).intValue(); 1820 | args.set(i, new Integer(tmp1 * tmp2)); 1821 | args.remove(i + 1); 1822 | oper.remove(i); 1823 | i --; 1824 | } 1825 | else if (((String)oper.get(i)).equals("/")) { 1826 | int tmp1 = ((Integer)args.get(i)).intValue(); 1827 | int tmp2 = ((Integer)args.get(i + 1)).intValue(); 1828 | args.set(i, new Integer(tmp1 / tmp2)); 1829 | args.remove(i + 1); 1830 | oper.remove(i); 1831 | i --; 1832 | } 1833 | 1834 | for (int i = 0; i < oper.size(); i++) 1835 | if (((String)oper.get(i)).equals("+")) { 1836 | int tmp1 = ((Integer)args.get(i)).intValue(); 1837 | int tmp2 = ((Integer)args.get(i + 1)).intValue(); 1838 | args.set(i, new Integer(tmp1 + tmp2)); 1839 | args.remove(i + 1); 1840 | oper.remove(i); 1841 | i --; 1842 | } 1843 | else if (((String)oper.get(i)).equals("-")) { 1844 | int tmp1 = ((Integer)args.get(i)).intValue(); 1845 | int tmp2 = ((Integer)args.get(i + 1)).intValue(); 1846 | args.set(i, new Integer(tmp1 - tmp2)); 1847 | args.remove(i + 1); 1848 | oper.remove(i); 1849 | i --; 1850 | } 1851 | 1852 | return ((Integer)args.get(0)).intValue(); 1853 | }// evalInt() 1854 | 1855 | private float evalReal(AdvLexem[] lex) { 1856 | ArrayList args = new ArrayList(); 1857 | ArrayList oper = new ArrayList(); 1858 | 1859 | while (place < lex.length) { 1860 | if (lex[place].code == Codes.RBRACKET) { 1861 | break; 1862 | } 1863 | if (lex[place].lcategory == Codes.NUMBER) 1864 | args.add(new Float(lex[place].value)); 1865 | else if (lex[place].code == Codes.VARIABLE) { 1866 | int tmp = vars.indexOf(lex[place].value); 1867 | int type = ((Integer)var_types.get(tmp)).intValue(); 1868 | if (type == Codes.INTEGER) 1869 | args.add(new Float(((Integer)var_vals.get(tmp)). 1870 | intValue())); 1871 | else 1872 | args.add((Float)var_vals.get(tmp)); 1873 | } 1874 | else if (lex[place].code == Codes.LBRACKET) { 1875 | place ++; 1876 | args.add(new Float(evalReal(lex))); 1877 | } 1878 | else if (lex[place].lcategory == Codes.AOPERATOR) 1879 | oper.add(lex[place].value); 1880 | place ++; 1881 | } 1882 | 1883 | for (int i = 0; i < oper.size(); i++) 1884 | if (((String)oper.get(i)).equals("*")) { 1885 | float tmp1 = ((Float)args.get(i)).floatValue(); 1886 | float tmp2 = ((Float)args.get(i + 1)).floatValue(); 1887 | args.set(i, new Float(tmp1 * tmp2)); 1888 | args.remove(i + 1); 1889 | oper.remove(i); 1890 | i --; 1891 | } 1892 | else if (((String)oper.get(i)).equals("/")) { 1893 | float tmp1 = ((Float)args.get(i)).floatValue(); 1894 | float tmp2 = ((Float)args.get(i + 1)).floatValue(); 1895 | args.set(i, new Float(tmp1 / tmp2)); 1896 | args.remove(i + 1); 1897 | oper.remove(i); 1898 | i --; 1899 | } 1900 | 1901 | for (int i = 0; i < oper.size(); i++) 1902 | if (((String)oper.get(i)).equals("+")) { 1903 | float tmp1 = ((Float)args.get(i)).floatValue(); 1904 | float tmp2 = ((Float)args.get(i + 1)).floatValue(); 1905 | args.set(i, new Float(tmp1 + tmp2)); 1906 | args.remove(i + 1); 1907 | oper.remove(i); 1908 | i --; 1909 | } 1910 | else if (((String)oper.get(i)).equals("-")) { 1911 | float tmp1 = ((Float)args.get(i)).floatValue(); 1912 | float tmp2 = ((Float)args.get(i + 1)).floatValue(); 1913 | args.set(i, new Float(tmp1 - tmp2)); 1914 | args.remove(i + 1); 1915 | oper.remove(i); 1916 | i --; 1917 | } 1918 | 1919 | return ((Float)args.get(0)).floatValue(); 1920 | }// evalReal() 1921 | 1922 | private boolean evalBool(AdvLexem[] lex) { 1923 | ArrayList args = new ArrayList(); 1924 | ArrayList oper = new ArrayList(); 1925 | ArrayList nots = new ArrayList(); 1926 | int pointer = 0; 1927 | 1928 | while (place < lex.length) { 1929 | if (lex[place].code == Codes.RBRACKET) { 1930 | break; 1931 | } 1932 | if (lex[place].code == Codes.VARIABLE) { 1933 | int tmp = vars.indexOf(lex[place].value); 1934 | int type = ((Integer)var_types.get(tmp)).intValue(); 1935 | if (type == Codes.BOOLEAN) 1936 | pointer = 1; 1937 | else 1938 | pointer = 2; 1939 | } 1940 | 1941 | if ((lex[place].code == Codes.TRUE) || 1942 | (lex[place].code == Codes.FALSE)) 1943 | args.add(new Boolean(lex[place].value)); 1944 | else if (pointer == 1) { 1945 | int tmp = vars.indexOf(lex[place].value); 1946 | args.add((Boolean)var_vals.get(tmp)); 1947 | pointer = 0; 1948 | } 1949 | else if (lex[place].code == Codes.LBRACKET) { 1950 | place ++; 1951 | args.add(new Boolean(evalBool(lex))); 1952 | } 1953 | else if ((lex[place].code == Codes.AND) || 1954 | (lex[place].code == Codes.OR)) 1955 | oper.add(lex[place].value); 1956 | else if (lex[place].code == Codes.NOT) 1957 | nots.add(new Integer(args.size())); 1958 | else { 1959 | float tmp1 = 0; 1960 | float tmp2 = 0; 1961 | int rel = 0; 1962 | if (lex[place].lcategory == Codes.NUMBER) 1963 | tmp1 = (new Float(lex[place].value)).floatValue(); 1964 | else if (pointer == 2) { 1965 | pointer = 0; 1966 | int tmp = vars.indexOf(lex[place].value); 1967 | int type = ((Integer)var_types.get(tmp)).intValue(); 1968 | if (type == Codes.INTEGER) 1969 | tmp1 = ((Integer)var_vals.get(tmp)).intValue(); 1970 | else 1971 | tmp1 = ((Float)var_vals.get(tmp)).floatValue(); 1972 | } 1973 | place ++; 1974 | rel = lex[place].code; 1975 | place ++; 1976 | if (lex[place].lcategory == Codes.NUMBER) 1977 | tmp2 = (new Float(lex[place].value)).floatValue(); 1978 | else if (lex[place].code == Codes.VARIABLE) { 1979 | int tmp = vars.indexOf(lex[place].value); 1980 | int type = ((Integer)var_types.get(tmp)).intValue(); 1981 | if (type == Codes.INTEGER) 1982 | tmp2 = ((Integer)var_vals.get(tmp)).intValue(); 1983 | else 1984 | tmp2 = ((Float)var_vals.get(tmp)).floatValue(); 1985 | } 1986 | boolean bool = false; 1987 | if (rel == Codes.E) 1988 | bool = tmp1 == tmp2; 1989 | else if (rel == Codes.NE) 1990 | bool = tmp1 != tmp2; 1991 | else if (rel == Codes.GT) { 1992 | bool = tmp1 > tmp2; 1993 | } 1994 | else if (rel == Codes.GE) 1995 | bool = tmp1 >= tmp2; 1996 | else if (rel == Codes.LT) 1997 | bool = tmp1 < tmp2; 1998 | else if (rel == Codes.LE) 1999 | bool = tmp1 <= tmp2; 2000 | 2001 | args.add(new Boolean(bool)); 2002 | } 2003 | 2004 | place ++; 2005 | } 2006 | 2007 | for (int i = 0; i < nots.size(); i++) { 2008 | int neg = ((Integer)nots.get(i)).intValue(); 2009 | boolean temp = ((Boolean)args.get(neg)).booleanValue(); 2010 | args.set(neg, new Boolean(!temp)); 2011 | } 2012 | 2013 | for (int i = 0; i < oper.size(); i++) 2014 | if (((String)oper.get(i)).equals("AND")) { 2015 | boolean tmp1 = ((Boolean)args.get(i)).booleanValue(); 2016 | boolean tmp2 = ((Boolean)args.get(i + 1)).booleanValue(); 2017 | args.set(i, new Boolean(tmp1 && tmp2)); 2018 | args.remove(i + 1); 2019 | oper.remove(i); 2020 | i --; 2021 | } 2022 | for (int i = 0; i < oper.size(); i++) 2023 | if (((String)oper.get(i)).equals("OR")) { 2024 | boolean tmp1 = ((Boolean)args.get(i)).booleanValue(); 2025 | boolean tmp2 = ((Boolean)args.get(i + 1)).booleanValue(); 2026 | args.set(i, new Boolean(tmp1 || tmp2)); 2027 | args.remove(i + 1); 2028 | oper.remove(i); 2029 | i --; 2030 | } 2031 | 2032 | return ((Boolean)args.get(0)).booleanValue(); 2033 | }// evalBool() 2034 | 2035 | //mmmmmmmmmmmmmmmmmmmmmmmmmmm EXECUTOR mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm 2036 | 2037 | private void execute(ArrayList al) { 2038 | for (int i = 0; i < al.size(); i++) 2039 | execLexem((ExecLexem)al.get(i)); 2040 | }// execute() 2041 | 2042 | private void execLexem(ExecLexem el) { 2043 | if (el.type.equals("IASSOC")) 2044 | execInt(el); 2045 | else if (el.type.equals("RASSOC")) 2046 | execReal(el); 2047 | else if (el.type.equals("BASSOC")) 2048 | execBool(el); 2049 | else if (el.type.equals("READ")) 2050 | execRead(el); 2051 | else if (el.type.equals("WRITE")) 2052 | execWrite(el); 2053 | else if (el.type.equals("WHILE")) 2054 | execWhile(el); 2055 | else if (el.type.equals("IF")) 2056 | execIf(el); 2057 | 2058 | }// execLexem() 2059 | 2060 | private void execInt(ExecLexem el) { 2061 | int tmp = vars.indexOf(el.variable); 2062 | place = 0; 2063 | var_vals.set(tmp, new Integer(evalInt(el.lexems))); 2064 | }// execInt() 2065 | 2066 | private void execReal(ExecLexem el) { 2067 | int tmp = vars.indexOf(el.variable); 2068 | place = 0; 2069 | var_vals.set(tmp, new Float(evalReal(el.lexems))); 2070 | }// execReal() 2071 | 2072 | private void execBool(ExecLexem el) { 2073 | int tmp = vars.indexOf(el.variable); 2074 | place = 0; 2075 | var_vals.set(tmp, new Boolean(evalBool(el.lexems))); 2076 | }// execBool() 2077 | 2078 | private void execWhile(ExecLexem el) { 2079 | place = 0; 2080 | 2081 | while (evalBool(el.lexems)) { 2082 | execute(el.iftrue); 2083 | place = 0; 2084 | } 2085 | }// execWhile() 2086 | 2087 | private void execIf(ExecLexem el) { 2088 | place = 0; 2089 | 2090 | if (evalBool(el.lexems)) 2091 | execute(el.iftrue); 2092 | else 2093 | execute(el.ifelse); 2094 | }// execIf() 2095 | 2096 | private void execRead(ExecLexem el) { 2097 | int tmp = vars.indexOf(el.lexems[1].value); 2098 | int type = ((Integer)var_types.get(tmp)).intValue(); 2099 | String s = ""; 2100 | 2101 | try { 2102 | s = activity.inputText(); 2103 | }catch (Exception ex) { 2104 | err("Error during input operation !"); } 2105 | 2106 | if (type == Codes.INTEGER) 2107 | var_vals.set(tmp, new Integer(s)); 2108 | else if (type == Codes.REAL) 2109 | var_vals.set(tmp, new Float(s)); 2110 | else 2111 | var_vals.set(tmp, new Boolean(s)); 2112 | 2113 | }// execRead() 2114 | 2115 | private void execWrite(ExecLexem el) { 2116 | for (int i = 1; i < el.lexems.length; i = i + 2) 2117 | if (el.lexems[i].code == Codes.STRING) 2118 | stdout += el.lexems[i].value; 2119 | else { 2120 | int tmp = vars.indexOf(el.lexems[i].value); 2121 | int type = ((Integer)var_types.get(tmp)).intValue(); 2122 | String s = ""; 2123 | 2124 | if (type == Codes.INTEGER) 2125 | s = ((Integer)var_vals.get(tmp)).toString(); 2126 | else if (type == Codes.REAL) 2127 | s = ((Float)var_vals.get(tmp)).toString(); 2128 | else 2129 | s = ((Boolean)var_vals.get(tmp)).toString(); 2130 | stdout += s; 2131 | } 2132 | stdout += "\n"; 2133 | }// execWrite() 2134 | 2135 | private void err(String msg){ 2136 | stderr += msg + "\n"; 2137 | } 2138 | 2139 | }// class Scanner 2140 | 2141 | //mmmmmmmmmmmmmmmmmmmm ADDITIONAL CLASSES mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm 2142 | 2143 | class Lexem { 2144 | String value; 2145 | byte type; 2146 | int line; 2147 | int column; 2148 | 2149 | public Lexem(String value, byte type, int line, int column) { 2150 | if (type != Codes.STRING) 2151 | this.value = value.toUpperCase(); 2152 | else 2153 | this.value = value; 2154 | this.type = type; 2155 | this.line = line; 2156 | this.column = column; 2157 | } 2158 | 2159 | public Lexem(StringBuffer value, byte type, int line, int column) { 2160 | if (type != Codes.STRING) 2161 | this.value = value.toString().toUpperCase(); 2162 | else 2163 | this.value = value.toString(); 2164 | this.type = type; 2165 | this.line = line; 2166 | this.column = column; 2167 | } 2168 | 2169 | public Lexem(int value, int type, int line, int column) { 2170 | char[] sign = new char[1]; 2171 | sign[0] = (char)value; 2172 | this.value = new String(sign); 2173 | this.type = (byte)type; 2174 | this.line = line; 2175 | this.column = column; 2176 | } 2177 | }// class Lexem 2178 | 2179 | class AdvLexem { 2180 | String value; 2181 | byte code; 2182 | byte lcategory; // lower category 2183 | byte hcategory; // higher category 2184 | byte ccategory; // custom category, if needed 2185 | int line; 2186 | int column; 2187 | 2188 | public AdvLexem(String value, byte code, byte lcategory, 2189 | byte hcategory, int line, int column) { 2190 | this.value = value; 2191 | this.code = code; 2192 | this.lcategory = lcategory; 2193 | this.hcategory = hcategory; 2194 | this.line = line; 2195 | this.column = column; 2196 | }// AdvLexem() 2197 | }// class AdvLexem 2198 | 2199 | class ExecLexem { 2200 | String type; // instruction type 2201 | String variable; // name of variable in association instruction 2202 | AdvLexem[] lexems; // lexems of the given instruction 2203 | ArrayList iftrue; 2204 | ArrayList ifelse; 2205 | 2206 | ExecLexem(String type, String variable, ArrayList AL, 2207 | int start, int finish, 2208 | ArrayList iftrue, ArrayList ifelse) { 2209 | this.type = type; 2210 | this.variable = variable; 2211 | lexems = new AdvLexem[finish - start + 1]; 2212 | for (int i = start, j = 0; i <= finish; i++, j++) 2213 | lexems[j] = (AdvLexem)AL.get(i); 2214 | this.iftrue = iftrue; 2215 | this.ifelse = ifelse; 2216 | }// ExecLexem() 2217 | }// class ExecLexem 2218 | --------------------------------------------------------------------------------